PostgreSQL查询服务器

一个基于MCP协议的PostgreSQL只读查询服务器,支持Claude Code、Cursor等AI工具直接查询数据库。

  • by avantifellows
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 avantifellows/mcp-postgres

使用说明

Avanti研究员PostgreSQL MCP服务器

用于查询Avanti Fellows数据库服务(PostgreSQL)的只读MCP服务器。

这是什么?

这是一个 MCP(模型上下文协议) 服务器,允许Claude Code、Cursor和其他人工智能工具直接查询我们的PostgreSQL数据库。它是 只读的 -不允许写入。

可用工具

工具 说明
query 运行任何SELECT查询
list_tables 列出数据库中的所有表
describe_table 获取列信息、主键、外键
sample_data 从表中获取示例行
count_rows 使用可选的WHERE子句计数行
search_columns 在所有表中按名称查找列

安装

1.安装包装

pip install git+https://github.com/avantifellows/mcp-postgres.git

2.设置凭据

将这些添加到您的shell配置文件中(~/.zshrc 或 ~/.bashrc):

export AF_DB_HOST="af-database-host"
export AF_DB_PORT="5432"
export AF_DB_USER="your_username"
export AF_DB_PASSWORD="your_password"
export AF_DB_NAME="database_name"

然后重新加载: source ~/.zshrc

3.配置您的AI工具

克劳德代码

claude mcp add avanti-db -- avanti-mcp-postgres

验证它是否正常工作:

claude mcp list

光标

增添 ~/.cursor/mcp.json:

{
  "mcpServers": {
    "avanti-db": {
      "command": "avanti-mcp-postgres",
      "env": {
        "AF_DB_HOST": "af-database-host",
        "AF_DB_PORT": "5432",
        "AF_DB_USER": "your_username",
        "AF_DB_PASSWORD": "your_password",
        "AF_DB_NAME": "database_name"
      }
    }
  }
}

使用示例

配置后,只需向Claude/Cursor询问数据库:

"What tables are in the database?"
"Show me the schema for the students table"
"How many users signed up this month?"
"Find all columns with 'email' in the name"
"What does the enrollment data look like?"

更新中

pip install --upgrade git+https://github.com/avantifellows/mcp-postgres.git

地方发展

# Clone the repo
git clone https://github.com/avantifellows/mcp-postgres.git
cd mcp-postgres

# Create virtual environment
python -m venv venv
source venv/bin/activate

# Install in dev mode
pip install -e .

# Create .env file with credentials
cp .env.example .env
# Edit .env with your credentials

# Test the server
avanti-mcp-postgres

安全说明

  • 此服务器是 只读的 -INSERT、UPDATE、DELETE被阻止
  • 使用您的个人数据库凭据
  • 查询会在本地记录,但不会发送到任何地方
  • 不要将凭据提交到git

故障排除

“连接被拒绝”

  • 检查 AF_DB_HOST 正确且可从您的网络访问
  • 如果需要,请验证您是否使用VPN

“身份验证失败”

  • 复查 AF_DB_USER 和 AF_DB_PASSWORD
  • 确保您的用户具有SELECT权限

克劳德代码中未显示服务器

  • 跑 claude mcp list 检查状态
  • 尝试删除并重新添加: claude mcp remove avanti-db && claude mcp add avanti-db -- avanti-mcp-postgres