Back to MCP directory
publicPublicdnsLocal runtime

postgres-mcp

一个提供PostgreSQL数据库只读访问的MCP服务器,支持查询执行和模式查看。

article

README

🚀 PostgreSQL 模型上下文协议 (Model Context Protocol) 服务器

本服务器提供对 PostgreSQL 数据库的只读访问功能。借助它,大型语言模型(LLMs)能够查看数据库架构并执行只读查询,为数据交互提供了便捷的途径。

✨ 主要特性

  • 为大型语言模型提供对 PostgreSQL 数据库的只读访问。
  • 允许大型语言模型查看数据库架构。
  • 支持大型语言模型执行只读查询。

📦 安装指南

Docker

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

💻 使用示例

工具

query

  • 功能:执行针对连接数据库的只读 SQL 查询。
  • 输入:sql (字符串),即要执行的 SQL 查询。
  • 注意:所有查询都在 READ ONLY 事务中执行。

资源

服务器提供每个表的架构信息,具体如下:

  • 表架构 (postgres://<host>/<table>/schema)
    • 包含每个表的 JSON 架构信息。
    • 涵盖列名和数据类型。
    • 这些信息会自动从数据库元数据中发现。

使用 Claude Desktop 的方法

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

Docker

⚠️ 重要提示

  • 当在 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 替换为您的数据库名称。

📄 许可证

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

help

Runtime guide

cloud

Hosted runtime

Hosted servers run from a provider-managed environment. You usually connect the MCP client to the hosted endpoint or follow the provider's authorization flow, without keeping a local process alive

  1. Open provider connection page
  2. Authorize or copy endpoint
  3. Connect from your MCP client
terminal

Local runtime / other methods

Local servers run on your own machine or infrastructure. You normally copy the server_config into your MCP client, install the required package, and provide env variables from env_schema when needed

  1. Copy server_config
  2. Install required package
  3. Fill env variables and restart client