使用说明
PostgreSQL MCP服务器
PostgreSQL数据库的模型控制协议(MCP)服务器,允许Claude与您的PostgreSQL数据库交互。
使用Claude代码
使用Claude Code使用此服务器的最简单方法:
# Generate MCP config and add it to Claude
JSON=$(postgres-mcp-server --json --connection-string '...') \
&& claude mcp add-json "$JSON"
安装
使用自制咖啡
# Add the tap
brew tap mgorunuch/postgres-mcp-server https://github.com/mgorunuch/postgres-mcp-server
# Install the server
brew install mgorunuch/postgres-mcp-server/postgres-mcp-server
手动安装
从发布页面下载适合您平台的二进制文件。
设置
有两种方法可以使用此服务器:
1.直接跑步
-
将PostgreSQL连接字符串设置为环境变量(可选):
export POSTGRES_CONNECTION_STRING="postgresql://user:password@host:port/dbname?sslmode=disable"如果未提供,则默认为
postgresql://postgres:postgres@localhost:5432/postgres?sslmode=disable -
启动服务器:
postgres-mcp-server -
通过设置与此服务器的MCP连接与Claude一起使用。
2.使用Claude的MCP配置
-
生成MCP配置JSON:
postgres-mcp-server --json --connection-string "postgresql://user:password@host:port/dbname?sslmode=disable"这将输出一个JSON配置,您可以使用Claude的MCP工具。
输出示例:
{"type":"stdio","command":"/path/to/postgres-mcp-server","args":["--connection-string","postgresql://user:password@host:port/dbname?sslmode=disable"],"env":{}} -
复制此JSON输出,并使用它在Claude中配置MCP连接。
命令行选项
--version:显示版本信息--json:输出MCP配置JSON以供Claude使用--connection-string:PostgreSQL连接字符串(覆盖环境变量)
安全
为了安全起见,默认情况下会阻止潜在的破坏性查询(DROP、TRUNCATE、DELETE、UPDATE、ALTER、CREATE、INSERT)。
要执行这些查询,您必须显式设置 unsafe: true 使用pgquery工具时。
