Skip to main content
Glama

PostgreSQL MCP Server

by gldc

PostgreSQL MCP 服务器

使用模型上下文协议 (MCP) Python SDK 实现的 PostgreSQL MCP 服务器。MCP 是一种开放协议,可实现 LLM 应用程序与外部数据源之间的无缝集成。该服务器允许 AI 代理通过标准化接口与 PostgreSQL 数据库交互。

特征

  • 列出数据库架构
  • 列出架构内的表
  • 描述表结构
  • 列出表约束和关系
  • 获取外键信息
  • 执行 SQL 查询

快速入门

# Run the server without a DB connection (useful for Glama or inspection) python postgres_server.py # With a live database – pick one method: export POSTGRES_CONNECTION_STRING="postgresql://user:pass@host:5432/db" python postgres_server.py # …or… python postgres_server.py --conn "postgresql://user:pass@host:5432/db" # Or using Docker (build once, then run): # docker build -t mcp-postgres . && docker run -p 8000:8000 mcp-postgres

安装

通过 Smithery 安装

要通过Smithery自动为 Claude Desktop 安装 PostgreSQL MCP 服务器:

npx -y @smithery/cli install @gldc/mcp-postgres --client claude

手动安装

  1. 克隆此存储库:
git clone <repository-url> cd mcp-postgres
  1. 创建并激活虚拟环境(推荐):
python -m venv venv source venv/bin/activate # On Windows, use: venv\Scripts\activate
  1. 安装依赖项:
pip install -r requirements.txt

用法

  1. 启动 MCP 服务器。
    # Without a connection string (server starts, DB‑backed tools will return a friendly error) python postgres_server.py # Or set the connection string via environment variable: export POSTGRES_CONNECTION_STRING="postgresql://username:password@host:port/database" python postgres_server.py # Or pass it using the --conn flag: python postgres_server.py --conn "postgresql://username:password@host:port/database"
  2. 该服务器提供以下工具:
  • query :对数据库执行 SQL 查询
  • list_schemas :列出所有可用的模式
  • list_tables :列出特定模式中的所有表
  • describe_table :获取有关表结构的详细信息
  • get_foreign_keys :获取表的外键关系
  • find_relationships :发现表的显式和隐式关系

使用 Docker 运行

构建图像:

docker build -t mcp-postgres .

在没有数据库连接的情况下运行容器(服务器保持可检查):

docker run -p 8000:8000 mcp-postgres

通过提供POSTGRES_CONNECTION_STRING使用实时 PostgreSQL 数据库运行:

docker run \ -e POSTGRES_CONNECTION_STRING="postgresql://username:password@host:5432/database" \ -p 8000:8000 \ mcp-postgres

如果省略环境变量,服务器将正常启动,并且所有数据库支持的工具都会返回友好的“未设置连接字符串”消息,直到您提供它。

使用 mcp.json 进行配置

要将此服务器与 MCP 兼容工具(如 Cursor)集成,请将其添加到您的~/.cursor/mcp.json

{ "servers": { "postgres": { "command": "/path/to/venv/bin/python", "args": [ "/path/to/postgres_server.py" ], "env": { "POSTGRES_CONNECTION_STRING": "postgresql://username:password@host:5432/database?ssl=true" } } } }

如果省略了POSTGRES_CONNECTION_STRING ,服务器仍会启动并完全可检查;数据库支持的工具将仅返回信息错误,直到提供该变量。

代替:

  • /path/to/venv替换为您的虚拟环境路径
  • /path/to/postgres_server.py包含服务器脚本的绝对路径

安全

  • 切勿在代码中暴露敏感的数据库凭据
  • 使用环境变量或安全配置文件作为数据库连接字符串
  • 考虑使用连接池来更好地管理资源
  • 实施适当的访问控制和用户身份验证

贡献

欢迎贡献代码!欢迎提交 Pull 请求。

相关项目

执照

MIT 许可证

版权所有 (c) 2025 gldc

特此授予获得此软件和相关文档文件(“软件”)副本的任何人免费许可,以无限制方式处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或销售软件副本的权利,并允许向其提供软件的人员这样做,但须遵守以下条件:

上述版权声明和本许可声明均应包含在软件的所有副本或实质性部分中。

本软件按“原样”提供,不附带任何形式的明示或暗示保证,包括但不限于适销性、适用于特定用途和非侵权性的保证。在任何情况下,作者或版权所有者均不对因本软件或使用或以其他方式处理本软件而引起的或与之相关的任何索赔、损害或其他责任承担责任,无论是合同、侵权或其他诉讼。

-
security - not tested
A
license - permissive license
-
quality - not tested

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

使 AI 代理能够通过模型上下文协议与 PostgreSQL 数据库交互,提供数据库模式探索、表结构检查和 SQL 查询执行功能。

  1. 特征
    1. 快速入门
      1. 安装
        1. 通过 Smithery 安装
        2. 手动安装
      2. 用法
        1. 使用 Docker 运行
        2. 使用 mcp.json 进行配置
      3. 安全
        1. 贡献
          1. 相关项目
            1. 执照

              Related MCP Servers

              • -
                security
                F
                license
                -
                quality
                A server that allows AI models to interact with PostgreSQL databases through a standardized protocol, providing database schema information and SQL query execution capabilities.
                Last updated -
                JavaScript
              • -
                security
                A
                license
                -
                quality
                A Model Context Protocol server that enables powerful PostgreSQL database management capabilities including analysis, schema management, data migration, and monitoring through natural language interactions.
                Last updated -
                355
                46
                TypeScript
                AGPL 3.0
                • Linux
                • Apple
              • A
                security
                A
                license
                A
                quality
                Enables AI models to interact with PostgreSQL databases through a standardized interface, supporting operations like queries, table manipulation, and schema inspection.
                Last updated -
                5
                127
                2
                JavaScript
                MIT License
              • -
                security
                F
                license
                -
                quality
                A Model Context Protocol server that enables interaction with PostgreSQL databases to list tables, retrieve schemas, and execute read-only SQL queries.
                Last updated -
                16,948
                JavaScript
                • Linux
                • Apple

              View all related MCP servers

              MCP directory API

              We provide all the information about MCP servers via our MCP API.

              curl -X GET 'https://glama.ai/api/mcp/v1/servers/gldc/mcp-postgres'

              If you have feedback or need assistance with the MCP directory API, please join our Discord server