PostgreSQL数据库连接服务

一个连接PostgreSQL数据库并提供模式检查和安全查询执行的MCP服务器,适用于需要数据库交互的开发场景。

  • by garvjoshi1111
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 garvjoshi1111/mcp-postgres

使用说明

mcp postgres

一个MCP服务器,连接到PostgreSQL数据库,并将模式检查和安全查询执行作为工具和资源公开,随时可以插入Claude Desktop或任何兼容MCP的客户端。

工具

工具 说明
list_tables 列出公共架构中的所有表
describe_table(table_name) 显示表的列、类型和可空性
run_query(query) 执行SELECT查询并返回结果(可变语句被阻止)

资源

URI 描述
schema://tables 完整模式转储——所有表及其列

设置

1.克隆并安装

git clone <repo-url>
cd mcp-postgres
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -e .

2.配置环境

cp .env.example .env
# Edit .env — set DATABASE_URL to your PostgreSQL connection string

3.运行服务器

mcp-postgres

或者直接:

python -m mcp_postgres.server

Claude桌面集成

添加到您的 claude_desktop_config.json:

{
  "mcpServers": {
    "postgres": {
      "command": "/path/to/.venv/bin/mcp-postgres",
      "env": {
        "DATABASE_URL": "postgresql://user:password@localhost:5432/mydb"
      }
    }
  }
}

项目结构

src/mcp_postgres/
  __init__.py   — package marker
  db.py         — psycopg2 connection context manager
  server.py     — FastMCP server, tools, and resources

安全

run_query 拒绝任何不以开头的陈述 SELECT这防止了通过MCP接口意外或恶意修改数据。