Back to MCP directory
publicPublicdnsLocal runtime

mcp-pg-schema

PostgreSQL的只读查询MCP服务

article

README

🚀 PostgreSQL 模型上下文协议服务器

本服务器基于模型上下文协议(Model Context Protocol),可提供对 PostgreSQL 数据库的只读访问服务。借助该服务器,大型语言模型(LLM)能够检查数据库架构,并执行只读查询操作。

🚀 快速开始

要将此服务器与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替换为您的数据库名称。

✨ 主要特性

工具

  • query
    • 可针对已连接的数据库执行只读 SQL 查询。
    • 输入参数:sql (字符串),即要执行的 SQL 查询语句。
    • 所有查询均在只读事务中执行。

资源

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

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

📦 安装指南

Docker 构建

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

📄 许可证

此 MCP 服务器遵循 MIT 许可证发布。您可以自由使用、修改和分发该软件,但需遵守 MIT 许可证的相关条款和条件。更多详细信息,请查阅项目存储库中的 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