首页
学习
活动
专区
圈层
工具
发布
MCP广场 >详情页
PostgreSQL MCP服务器2026-06-07368分享添加福利群:解决AI开发者的「MCP实战痛点」
该MCP服务器是一个专为LLMs设计的PostgreSQL只读访问工具,允许模型检查数据库模式并执行安全查询。核心功能包括:1) 提供表结构的JSON模式信息;2) 支持通过SQL查询数据库(所有操作在只读事务中执行);3) 提供多种集成方式(Docker、NPX、VS Code等)。适用于需要数据库查询能力的AI应用场景,配置灵活且开源。
By modelcontextprotocol
2026-06-07368
详情内容

PostgreSQL

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

组件

工具

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

资源

服务器提供数据库中每个表的模式信息:

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

配置

与 Claude Desktop 一起使用

要在 Claude Desktop 应用程序中使用此服务器,请将以下配置添加到 claude_desktop_config.json 的 "mcpServers" 部分:

Docker

  • 在 macOS 上运行 Docker 时,如果服务器在主机网络上运行(例如 localhost),请使用 host.docker.internal
  • 用户名/密码可以通过 postgresql://user:password@host:port/db-name 添加到 PostgreSQL URL 中
{
  "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 Code 一起使用

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

在 VS Code 中使用 NPX 安装 在 VS Code Insiders 中使用 NPX 安装

在 VS Code 中使用 Docker 安装 在 VS Code Insiders 中使用 Docker 安装

对于手动安装,请将以下 JSON 块添加到 VS Code 的用户设置(JSON)文件中。您可以通过按 Ctrl + Shift + P 并输入 Preferences: Open User Settings (JSON) 来完成此操作。

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

请注意,.vscode/mcp.json 文件中不需要 mcp 键。

Docker

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

{
  "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 文件。

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档