这是一个基于 Model Context Protocol (MCP) 的服务器,用于与包含产品信息的 PostgreSQL 数据库交互。它允许客户端(例如 AI 助手)使用 SQL 查询数据库,为数据交互提供了便捷的方式。
git clone https://github.com/michael7736/postgres-mcp-server.git
cd postgres-mcp-server
npm install
# 或者
yarn install
将 TypeScript 代码编译为 JavaScript:
npm run build
# 或者
yarn build
这将在 build/
目录中创建可执行文件(build/index.js
)。
此服务器需要通过环境变量提供 PostgreSQL 连接详细信息,这些信息在您的 MCP 客户端设置中配置时会用到(例如 cline_mcp_settings.json
或 claude_desktop_config.json
)。
所需的环境变量包括:
PGHOST
:PostgreSQL 服务器的主机名。PGPORT
:PostgreSQL 服务器的端口号(默认:5432)。PGUSER
:数据库连接的用户名。PGPASSWORD
:数据库连接密码。PGDATABASE
:要连接到的数据库名称。示例 MCP 设置配置:
{
"mcpServers": {
"postgres-products": {
"command": "node",
"args": ["/path/to/your/postgres-mcp-server/build/index.js"],
"env": {
"PGHOST": "your_db_host",
"PGPORT": "5432",
"PGUSER": "your_db_user",
"PGPASSWORD": "your_db_password",
"PGDATABASE": "your_db_name"
},
"disabled": false, // 确保已启用
"autoApprove": [] // 配置自动批准如果需要
}
// ... 其他服务器
}
}
将 /path/to/your/postgres-mcp-server/build/index.js
替换为您系统上实际构建文件的路径,并在 env
部分填写您的具体数据库凭据。
配置完成后并通过您的 MCP 客户端运行此服务器,该服务器提供以下工具:
run_sql_query
执行针对配置的产品数据库的只读 SQL 查询(仅限 SELECT 语句)。
输入模式:
{
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "要执行的 SQL 选择查询。"
}
},
"required": ["query"]
}
示例工具调用:
<use_mcp_tool>
<server_name>postgres-productsserver_name>
<tool_name>run_sql_querytool_name>
<arguments>
{
"query": "SELECT product_name, stock_quantity FROM products WHERE category = 'Electronics';"
}
arguments>
use_mcp_tool>