PostgreSQL AI服务
使AI模型能够通过标准化接口与PostgreSQL数据库进行交互,支持查询、表操作和模式检查等操作。
MCP 服务配置
复制以下 JSON 到 OPClaw 或其他 MCP 客户端的配置文件中即可使用
{
"mcpServers": {
"postgres": {
"args": [
"-y",
"mcp-postgres-server"
],
"command": "npx",
"env": {
"PG_DATABASE": "your_database",
"PG_HOST": "your_host",
"PG_PASSWORD": "your_password",
"PG_PORT": "5432",
"PG_USER": "your_user"
},
"type": "stdio"
}
}
}
该服务需要配置环境变量:PG_DATABASE、PG_HOST、PG_PASSWORD、PG_PORT、PG_USER
可用工具 (5 个)
该服务在 MCP 协议中暴露的工具,AI 可按需调用
connect_db 5 个参数 需填 4 项
Connect to PostgreSQL database. NOTE: Default connection exists - only use when requested or if other commands fail
必填参数:host、user、password、database
query 2 个参数 需填 1 项
Execute a SELECT query
必填参数:sql
execute 2 个参数 需填 1 项
Execute an INSERT, UPDATE, or DELETE query
必填参数:sql
list_tables
List all tables in the database
该工具无需必填参数,直接调用即可
describe_table 1 个参数 需填 1 项
Get table structure
必填参数:table
服务介绍
MCP PostgreSQL 服务器
一个提供 PostgreSQL 数据库操作的模型上下文协议服务器。该服务器使 AI 模型能够通过标准化接口与 PostgreSQL 数据库进行交互。
安装
手动安装
npm install mcp-postgres-server
或者直接运行:
npx mcp-postgres-server
配置
服务器需要以下环境变量:
{
"mcpServers": {
"postgres": {
"type": "stdio",
"command": "npx",
"args": ["-y", "mcp-postgres-server"],
"env": {
"PG_HOST": "your_host",
"PG_PORT": "5432",
"PG_USER": "your_user",
"PG_PASSWORD": "your_password",
"PG_DATABASE": "your_database"
}
}
}
}
可用工具
1. connect_db
使用提供的凭据建立与 PostgreSQL 数据库的连接。
use_mcp_tool({
server_name: "postgres",
tool_name: "connect_db",
arguments: {
host: "localhost",
port: 5432,
user: "your_user",
password: "your_password",
database: "your_database"
}
});
2. query
执行带有可选预处理语句参数的 SELECT 查询。支持 PostgreSQL 风格($1, $2)和 MySQL 风格(?)的参数占位符。
use_mcp_tool({
server_name: "postgres",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = $1",
params: [1]
}
});
3. execute
执行带有可选预处理语句参数的 INSERT、UPDATE 或 DELETE 查询。支持 PostgreSQL 风格($1, $2)和 MySQL 风格(?)的参数占位符。
use_mcp_tool({
server_name: "postgres",
tool_name: "execute",
arguments: {
sql: "INSERT INTO users (name, email) VALUES ($1, $2)",
params: ["John Doe", "john@example.com"]
}
});
4. list_schemas
列出已连接数据库中的所有模式。
use_mcp_tool({
server_name: "postgres",
tool_name: "list_schemas",
arguments: {}
});
5. list_tables
列出已连接数据库中的表。接受一个可选的模式参数(默认为 'public')。
// List tables in the 'public' schema (default)
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {}
});
// List tables in a specific schema
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {
schema: "my_schema"
}
});
6. describe_table
获取特定表的结构。接受一个可选的模式参数(默认为 'public')。
// Describe a table in the 'public' schema (default)
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
// Describe a table in a specific schema
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users",
schema: "my_schema"
}
});
特性
- 安全的连接处理,具有自动清理功能
- 支持预处理语句以处理查询参数
- 同时支持 PostgreSQL 风格($1, $2)和 MySQL 风格(?)的参数占位符
- 全面的错误处理和验证
- TypeScript 支持
- 自动连接管理
- 支持 PostgreSQL 特有的语法和特性
- 多模式支持,适用于数据库操作
安全性
- 使用预处理语句防止 SQL 注入
- 通过环境变量支持安全密码处理
- 在执行前验证查询
- 完成后自动关闭连接
错误处理
服务器为常见问题提供了详细的错误信息:
- 连接失败
- 无效查询
- 缺少参数
- 数据库错误
许可证
MIT