这是一个提供PostgreSQL数据库操作的Model Context Protocol服务器。该服务器使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"
}
}
}
}
使用提供的凭据连接到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"
}
});
执行带有可选预处理语句参数的SELECT查询。支持PostgreSQL风格($1, $2)和MySQL风格(?)的参数占位符。
use_mcp_tool({
server_name: "postgres",
tool_name: "query",
arguments: {
sql: "SELECT * FROM users WHERE id = $1",
params: [1]
}
});
执行带有可选预处理语句参数的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"]
}
});
列出连接数据库中的所有模式。
use_mcp_tool({
server_name: "postgres",
tool_name: "list_schemas",
arguments: {}
});
列出连接数据库中的表。接受一个可选的模式参数(默认为'public')。
// 列出'public'模式下的表(默认)
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {}
});
// 列出特定模式下的表
use_mcp_tool({
server_name: "postgres",
tool_name: "list_tables",
arguments: {
schema: "my_schema"
}
});
获取特定表的结构。接受一个可选的模式参数(默认为'public')。
// 描述'public'模式下的表(默认)
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users"
}
});
// 描述特定模式下的表
use_mcp_tool({
server_name: "postgres",
tool_name: "describe_table",
arguments: {
table: "users",
schema: "my_schema"
}
});
服务器为常见问题提供了详细的错误消息:
MIT