PostgreSQL MCP 服务器
使用模型上下文协议 (MCP) Python SDK 实现的 PostgreSQL MCP 服务器。MCP 是一种开放协议,可实现 LLM 应用程序与外部数据源之间的无缝集成。该服务器允许 AI 代理通过标准化接口与 PostgreSQL 数据库交互。
特征
列出数据库架构
列出架构内的表
描述表结构
列出表约束和关系
获取外键信息
执行 SQL 查询
快速入门
安装
通过 Smithery 安装
要通过Smithery自动为 Claude Desktop 安装 PostgreSQL MCP 服务器:
手动安装
克隆此存储库:
创建并激活虚拟环境(推荐):
安装依赖项:
用法
启动 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"该服务器提供以下工具:
query
:对数据库执行 SQL 查询list_schemas
:列出所有可用的模式list_tables
:列出特定模式中的所有表describe_table
:获取有关表结构的详细信息get_foreign_keys
:获取表的外键关系find_relationships
:发现表的显式和隐式关系
使用 Docker 运行
构建图像:
在没有数据库连接的情况下运行容器(服务器保持可检查):
通过提供POSTGRES_CONNECTION_STRING
使用实时 PostgreSQL 数据库运行:
如果省略环境变量,服务器将正常启动,并且所有数据库支持的工具都会返回友好的“未设置连接字符串”消息,直到您提供它。
使用 mcp.json 进行配置
要将此服务器与 MCP 兼容工具(如 Cursor)集成,请将其添加到您的~/.cursor/mcp.json
:
如果省略了
代替:
/path/to/venv
替换为您的虚拟环境路径/path/to/postgres_server.py
包含服务器脚本的绝对路径
安全
切勿在代码中暴露敏感的数据库凭据
使用环境变量或安全配置文件作为数据库连接字符串
考虑使用连接池来更好地管理资源
实施适当的访问控制和用户身份验证
贡献
欢迎贡献代码!欢迎提交 Pull 请求。
相关项目
执照
MIT 许可证
版权所有 (c) 2025 gldc
特此授予获得此软件和相关文档文件(“软件”)副本的任何人免费许可,以无限制方式处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或销售软件副本的权利,并允许向其提供软件的人员这样做,但须遵守以下条件:
上述版权声明和本许可声明均应包含在软件的所有副本或实质性部分中。
本软件按“原样”提供,不附带任何形式的明示或暗示保证,包括但不限于适销性、适用于特定用途和非侵权性的保证。在任何情况下,作者或版权所有者均不对因本软件或使用或以其他方式处理本软件而引起的或与之相关的任何索赔、损害或其他责任承担责任,无论是合同、侵权或其他诉讼。
This server cannot be installed
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 查询执行功能。
Related MCP Servers
- -securityFlicense-qualityA 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 -1
- AsecurityAlicenseAqualityEnables AI models to interact with PostgreSQL databases through a standardized interface, supporting operations like queries, table manipulation, and schema inspection.Last updated -61917MIT License
- -securityFlicense-qualityA Model Context Protocol server that provides AI assistants with secure, read-only access to PostgreSQL databases while offering comprehensive tools for schema exploration, query validation, and performance optimization.Last updated -
- AsecurityFlicenseAqualityEnables AI assistants to interact with PostgreSQL databases using natural language queries, providing secure read-only access to database schemas and SQL translation capabilities.Last updated -62