PostgreSQL MCP 服务器是一个用于与 PostgreSQL 数据库交互的 Model Context Protocol (MCP) 服务器,支持读取查询、写入查询、创建表和列出表等功能,为数据库操作提供了便捷的解决方案。
git clone https://github.com/yourusername/pgmcp.git
cd pgmcp
go mod tidy
make build
将以下环境变量添加到项目的 .env
文件中:
DB_NAME
: PostgreSQL 数据库名称DB_USER
: 连接数据库的用户名DB_PASSWORD
: 用户密码DB_HOST
: 数据库主机地址(默认为 localhost
)DB_PORT
: 数据库端口号(默认为 5432
)示例 .env 文件:
DB_NAME=postgres
DB_USER=pguser
DB_PASSWORD=pgpassword
DB_HOST=localhost
DB_PORT=5432
go run main.go
在 config.json
中配置 MCP 服务参数:
{
"name": "PostgreSQL MCP Server",
"version": "1.0",
"description": "A PostgreSQL MCP server implementation.",
"tools": {
"read_query": {
"description": "Execute a SELECT query and return the results."
},
"write_query": {
"description": "Execute an INSERT, UPDATE, or DELETE statement."
},
"create_table": {
"description": "Create a new table in the database."
},
"list_tables": {
"description": "List all tables in the database."
}
}
}
{
"tool": "read_query",
"params": {
"query": "SELECT * FROM users LIMIT 10;"
}
}
{
"tool": "write_query",
"params": {
"query": "INSERT INTO users (name, email) VALUES ($1, $2);",
"args": ["John Doe", "john@example.com"]
}
}
{
"tool": "create_table",
"params": {
"table_name": "users",
"schema": "CREATE TABLE users (\n id SERIAL PRIMARY KEY,\n name VARCHAR(255) NOT NULL,\n email VARCHAR(255) UNIQUE NOT NULL\n);"
}
}
{
"tool": "list_tables",
"params": {}
}
DB_SSLMODE
为 require
或更高。.env
文件[在此处添加许可信息]
[在此处添加贡献指南]