PostgreSQL MCP Server
PostgreSQL MCP 服务器
使用模型上下文协议 (MCP) Python SDK 实现的 PostgreSQL MCP 服务器。MCP 是一种开放协议,可实现 LLM 应用程序与外部数据源之间的无缝集成。该服务器允许 AI 代理通过标准化接口与 PostgreSQL 数据库交互。
特征
列出数据库架构
列出架构内的表
描述表结构
列出表约束和关系
获取外键信息
执行 SQL 查询
Related MCP server: PostgreSQL MCP Server
快速入门
# 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手动安装
克隆此存储库:
git clone <repository-url>
cd mcp-postgres创建并激活虚拟环境(推荐):
python -m venv venv
source venv/bin/activate # On Windows, use: venv\Scripts\activate安装依赖项:
pip install -r requirements.txt用法
启动 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 运行
构建图像:
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
特此授予获得此软件和相关文档文件(“软件”)副本的任何人免费许可,以无限制方式处理软件,包括但不限于使用、复制、修改、合并、发布、分发、再授权和/或销售软件副本的权利,并允许向其提供软件的人员这样做,但须遵守以下条件:
上述版权声明和本许可声明均应包含在软件的所有副本或实质性部分中。
本软件按“原样”提供,不附带任何形式的明示或暗示保证,包括但不限于适销性、适用于特定用途和非侵权性的保证。在任何情况下,作者或版权所有者均不对因本软件或使用或以其他方式处理本软件而引起的或与之相关的任何索赔、损害或其他责任承担责任,无论是合同、侵权或其他诉讼。
This server cannot be installed
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Related MCP Servers
- AlicenseBqualityFmaintenanceEnables AI models to interact with PostgreSQL databases through a standardized interface, supporting operations like queries, table manipulation, and schema inspection.650516MIT
- Alicense-qualityDmaintenanceEnables AI agents to query, explore, and analyze PostgreSQL databases through the Model Context Protocol. It provides robust security features including read-only mode, schema restrictions, and query timeouts for safe data interaction.11MIT
- Flicense-qualityDmaintenanceEnables AI assistants to interact with PostgreSQL databases by executing SQL queries and inspecting database schemas. It provides tools to facilitate database exploration and management through the Model Context Protocol.
- AlicenseAqualityCmaintenanceEnables AI assistants to query PostgreSQL databases, inspect schemas, and retrieve complete DDL with built-in read-only protection. It supports multiple database connections and allows for secure database interaction and exploration via the Model Context Protocol.5231MIT
Related MCP Connectors
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Comprehensive PostgreSQL documentation and best practices, including ecosystem tools
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
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