这是一个提供全面 PostgreSQL 数据库管理功能的模型上下文协议(MCP)服务器,适用于AI助手。
<a href="https://glama.ai/mcp/servers/@HenkDz/postgresql-mcp-server"> <img width="380" height="200" src="https://gips3.baidu.com/it/u=1815813476,3101388145&fm=3081&app=3_081&f=PNG?w=760&h=400" alt="PostgreSQL Server MCP 服务器" /> </a>🚀 新特性: 该服务器已从46个独立工具整合并增强为17个智能工具(34→8个元工具)和新增加了4个新工具,提供了更好的AI发现能力,并增加了强大的数据操作和注释管理功能。
# 全局安装
npm install -g @henkey/postgres-mcp-server
# 或直接使用npx运行(无需安装)
# 使用环境变量连接字符串(可选)
export POSTGRES_CONNECTION_STRING="postgresql://user:pass@localhost:5432/db"
npx @henkey/postgres-mcp-server
# 或直接传递:
npx @henkey/postgres-mcp-server --connection-string "postgresql://user:pass@localhost:5432/db"
npx @henkey/postgres-mcp-server --help
添加到您的MCP客户端配置中:
{
"mcpServers": {
"postgresql-mcp": {
"command": "npx",
"args": [
"@henkey/postgres-mcp-server",
"--connection-string", "postgresql://user:password@host:port/database"
]
}
}
}
npx -y @smithery/cli install @HenkDz/postgresql-mcp-server --client claude
# 构建Docker镜像
docker build -t postgres-mcp-server .
# 使用环境变量运行
docker run -i --rm \
-e POSTGRES_CONNECTION_STRING="postgresql://user:password@host:port/database" \
postgres-mcp-server
添加到您的MCP客户端配置中:
{
"mcpServers": {
"postgresql-mcp": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"henkey/postgres-mcp:latest",
"-e",
"POSTGRES_CONNECTION_STRING"
],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@host:port/database"
}
}
}
}
git clone <repository-url>
cd postgresql-mcp-server
npm install
npm run build
添加到您的MCP客户端配置中:
{
"mcpServers": {
"postgresql-mcp": {
"command": "node",
"args": [
"/path/to/postgresql-mcp-server/build/index.js",
"--connection-string", "postgresql://user:password@host:port/database"
]
}
}
}
17个强大工具,分为三类:
全新功能,不在原始46个工具中
// 分析数据库性能
{ "analysisType": "performance" }
// 创建带有约束的表
{
"operation": "create_table",
"tableName": "users",
"columns": [
{ "name": "id", "type": "SERIAL PRIMARY KEY" },
{ "name": "email", "type": "VARCHAR(255) UNIQUE NOT NULL" }
]
}
// 使用参数查询数据
{
"operation": "select",
"query": "SELECT * FROM users WHERE created_at > $1",
"parameters": ["2024-01-01"],
"limit": 100
}
// 插入新数据
{
"operation": "insert",
"table": "users",
"data": {"name": "John Doe", "email": "john@example.com"},
"returning": "*"
}
// 查找慢查询
{
"operation": "get_slow_queries",
"limit": 5,
"minDuration": 100
}
// 执行参数化的SELECT查询
{
"operation": "select",
"query": "SELECT * FROM users WHERE id = $1",
"parameters": [1]
}
// 执行INSERT变更
{
"operation": "insert",
"table": "products",
"data": {"name": "New Product", "price": 99.99},
"returning": "id"
}
// 管理数据库对象注释
{
"operation": "set",
"objectType": "table",
"objectName": "users",
"comment": "主要用户账户信息表"
}
📋 完整的工具模式参考 - 所有18个工具参数及示例一览
更多详细信息,请参阅docs/文件夹:
✅ 34→8个元工具 - 智能整合以提高AI发现能力 ✅ 每个工具多个操作 - 统一模式与操作参数 ✅ 智能参数验证 - 清晰错误消息和类型安全性
✅ 完整的CRUD操作 - 参数化查询支持的INSERT/UPDATE/DELETE/UPSERT ✅ 灵活查询 - 支持count/exists的SELECT操作和安全限制 ✅ 任意SQL执行 - 支持事务的复杂操作
✅ 灵活连接 - CLI参数、环境变量或每个工具配置 ✅ 注重安全 - 防止SQL注入,参数化查询 ✅ 健壮架构 - 连接池,全面错误处理
PostgreSQL MCP服务器完全兼容Docker,并可用于生产环境。
# 本地构建
docker build -t postgres-mcp-server .
# 或从Docker Hub拉取
docker pull henkey/postgres-mcp:latest
# 基本用法(使用Docker Hub镜像)
docker run -i --rm \
-e POSTGRES_CONNECTION_STRING="postgresql://user:password@host:port/database" \
henkey/postgres-mcp:latest
# 或使用本地构建的镜像
docker run -i --rm \
-e POSTGRES_CONNECTION_STRING="postgresql://user:password@host:port/database" \
postgres-mcp-server
# 带工具配置
docker run -i --rm \
-e POSTGRES_CONNECTION_STRING="postgresql://user:password@host:port/database" \
-e POSTGRES_TOOLS_CONFIG="/app/config/tools.json" \
-v /path/to/config:/app/config \
postgres-mcp-server
version: '3.8'
services:
postgres-mcp:
build: .
environment:
- POSTGRES_CONNECTION_STRING=postgresql://user:password@postgres:5432/database
depends_on:
- postgres
stdin_open: true
tty: true
postgres:
image: postgres:15
environment:
- POSTGRES_DB=database
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
ports:
- "5432:5432"
用于与MCP客户端如Cursor或Claude Desktop一起使用:
{
"mcpServers": {
"postgresql-mcp": {
"command": "docker",
"args": [
"run",
"-i",
"--rm",
"henkey/postgres-mcp:latest",
"-e",
"POSTGRES_CONNECTION_STRING"
],
"env": {
"POSTGRES_CONNECTION_STRING": "postgresql://user:password@host:port/database"
}
}
}
}
详见开发指南获取详细的设置说明。
AGPLv3许可证 - 详情见LICENSE文件。