PostgreSQL只读查询服务

提供对PostgreSQL数据库的只读访问,允许LLM检查数据库模式并执行只读查询。

  • by Davi-Schumacher
  • JavaScript
  • 本地部署
  • 更新于
在 GitHub 查看 davi-schumacher/postgres-mcp-server

使用说明

PostgreSQL

我从Anthropic现在存档的参考服务器中删除了这个,因为这是一个很好的起点,并将根据需要添加一些功能。此行以下的所有内容都与参考服务器仓库中的内容完全一致,但组件列表除外,如果添加了功能,我将更新这些列表。

一个模型上下文协议服务器,提供对PostgreSQL数据库的只读访问。此服务器使LLM能够检查数据库模式并执行只读查询。

组件

工具

  • 怎么翻译

    • 对连接的数据库执行只读SQL查询
    • 输入: sql (string):要执行的SQL查询
    • 所有查询都在只读事务中执行
  • list_tables

    • 打印所有可用表及其模式
    • 允许代理自行发现表及其模式,而不需要通过资源手动提供它们

资源

服务器为数据库中的每个表提供架构信息:

  • 持微软签名的表模式 (postgres://<host>/<table>/schema)
    • 每个表的JSON模式信息
    • 包括列名和数据类型
    • 从数据库元数据中自动发现

配置

使用Claude Desktop

要将此服务器与Claude Desktop应用程序一起使用,请将以下配置添加到您的“mcpServers”部分 claude_desktop_config.json:

码头工人

  • 在macos上运行docker时,如果服务器在主机网络上运行(例如localhost),请使用host.docker.internal
  • 用户名/密码可以添加到postgresql url中 postgresql://user:password@host:port/db-name
{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run", 
        "-i", 
        "--rm", 
        "mcp/postgres", 
        "postgresql://host.docker.internal:5432/mydb"]
    }
  }
}

NPX

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://localhost/mydb"
      ]
    }
  }
}

替换 /mydb 使用您的数据库名称。

使用VS代码

为了快速安装,请使用下面的一键安装按钮之一。..

对于手动安装,请将以下JSON块添加到VS Code中的用户设置(JSON)文件中。您可以按 Ctrl + Shift + P 和打字 Preferences: Open User Settings (JSON).

您可以选择将其添加到名为的文件中 .vscode/mcp.json 在您的工作空间中。这将允许您与他人共享配置。

请注意 mcp 中不需要密钥 .vscode/mcp.json 文件。

码头工人

备注:使用Docker并连接到主机上的PostgreSQL服务器时,请使用 host.docker.internal 而不是 localhost 在连接URL中。

{
  "mcp": {
    "inputs": [
      {
        "type": "promptString",
        "id": "pg_url",
        "description": "PostgreSQL URL (e.g. postgresql://user:pass@host.docker.internal:5432/mydb)"
      }
    ],
    "servers": {
      "postgres": {
        "command": "docker",
        "args": [
          "run",
          "-i",
          "--rm",
          "mcp/postgres",
          "${input:pg_url}"
        ]
      }
    }
  }
}

NPX

{
  "mcp": {
    "inputs": [
      {
        "type": "promptString",
        "id": "pg_url",
        "description": "PostgreSQL URL (e.g. postgresql://user:pass@localhost:5432/mydb)"
      }
    ],
    "servers": {
      "postgres": {
        "command": "npx",
        "args": [
          "-y",
          "@modelcontextprotocol/server-postgres",
          "${input:pg_url}"
        ]
      }
    }
  }
}

建筑

Docker:

docker build -t mcp/postgres -f src/postgres/Dockerfile . 

许可证

此MCP服务器根据MIT许可证获得许可。这意味着您可以根据MIT许可证的条款和条件自由使用、修改和分发软件。有关更多详细信息,请参阅项目存储库中的LICENSE文件。