PostgreSQL MCP 全访问权限
适用于生产的PostgreSQL MCP服务器,具备全局连接池、PgBouncer集成以及AWS RDS自动密码轮换功能。
快速入门(2分钟)
使用 AWS Secrets Manager
# 1. Set AWS credentials
export AWS_ACCESS_KEY_ID=your_key
export AWS_SECRET_ACCESS_KEY=your_secret
export AWS_REGION=us-west-2
# 2. Deploy (fetches DB credentials from AWS)
python3 deploy_with_aws_secrets.py
# 3. Restart Claude Code to connect
# The deploy script shows the URL - it's http://localhost:3000/sse搞定!MCP服务器现已启动,Claude Code 将自动检测到它。
可选: 设置自定义的AWS密钥名称:
export AWS_SECRET_NAME=your/secret/name
export AWS_PASSWORD_SECRET_NAME=rds!your-password-id
python3 deploy_with_aws_secrets.py不使用AWS(简单凭证)
# 1. Copy .env.example and fill in your database credentials
cd /path/to/postgres_mcp_allaccess
cp .env.example .env
# Edit .env and fill in the REQUIRED fields (first 5 lines)
# 2. Deploy (docker-compose reads .env automatically)
docker-compose up -d
# 3. Restart Claude Code to connect
# MCP server is at http://localhost:3000/sse你将获得
✅ 全局连接池 - 最多15个到PostgreSQL的连接(可配置) ✅ 自动密码轮换 - AWS轮换密码时无停机时间 ✅ 事务级池化 - 在所有查询中高效重用连接 ✅ 已准备好投入生产 - 内置健康检查、日志记录、监控功能
它是如何工作的
Multiple Claude Sessions → MCP Server → PgBouncer → PostgreSQL
(90+ clients) (max 15) (max 15) (sees ≤15 connections)- PgBouncer 强制限制最大 15 个连接 到您的 PostgreSQL 数据库
- 自动排队 - 额外请求暂存,待连接空闲时处理
- 无需配置 - 默认设置适用于大多数使用场景
常见任务
检查它是否正在运行
curl http://localhost:3000/health
# Should show: "OK - Pool: 2/15 connections"查看日志
docker-compose logs -f停止服务器
docker-compose down更改连接限制
编辑 pgbouncer/pgbouncer.ini:
default_pool_size = 10 # Max connections to PostgreSQL然后重新部署: docker-compose up -d --build
可用工具(在Claude中使用)
查询执行:
list_tables- 列出模式中的表execute_query- 运行任何SQL查询describe_table- 查看表结构execute_file- 从文件运行SQL
模式操作:
list_schemas- 列出所有模式search_tables- 按名称模式查找表get_database_context- 数据库结构概述
分析:
explain_query- 获取查询执行计划analyze_query- 性能分析suggest_indexes- 索引建议
会议/会话:
get_session_info- 当前会话状态get_query_history- 查看查询历史
故障排除
克劳德·科德无法连接
解决方案: 部署MCP服务器后,重启Claude代码。
“池未初始化”错误
解决方案: 首先运行任何查询 - 连接池在首次使用时初始化。
容器无法启动
# Check what went wrong
docker-compose logs
# Common fixes:
# 1. Port 3000 in use: Change MCP_SSE_PORT in .env
# 2. Bad credentials: Check .env file or AWS secrets
# 3. Build cache issue: docker-compose up -d --build --force-recreate连接超时
增加连接池超时时间(默认30秒):
export POSTGRES_POOL_TIMEOUT=60
python3 deploy_with_aws_secrets.py # or docker-compose up -d连接到 PostgreSQL 的连接数过多
检查你的限额:
docker exec -e PGPASSWORD='your_pass' postgres-mcp-allaccess \
psql -h localhost -p 6432 -U your_user -d your_db \
-c "SELECT count(*) FROM pg_stat_activity WHERE usename='your_user'"应显示2-15个连接(不会超过 default_pool_size)。
配置
连接池设置
环境变量 (在部署前设置):
POSTGRES_MAX_CONNECTIONS=15 # MCP pool size
POSTGRES_MIN_CONNECTIONS=2 # Warm connections
POSTGRES_POOL_TIMEOUT=30 # Queue wait time (seconds)PgBouncer 限制 (编辑 pgbouncer/pgbouncer.ini):
default_pool_size = 15 # MAX connections to PostgreSQL
min_pool_size = 2 # MIN kept alive
pool_mode = transaction # Release after each transaction了解这个水池
问题: 如果同时查询90个Claude会话,PostgreSQL会看到90个连接吗?
答案: 不! PostgreSQL 最多看到 15 个连接(或你设置的任意数量) default_pool_size)。
- 自动释放连接 每次查询后
- 额外请求队列 当连接释放时进行处理
- 无需会话管理 - 一切都是自动的
自动密码轮换
当AWS更换您的密码时:
- 下一个查询因认证错误而失败
- MCP 从 AWS Secrets Manager 获取新密码
- 更新MCP池和PgBouncer
- 重试查询 - 成功!
零停机时间。 无需重启。
要求:
- 使用(某工具/方法)进行部署
deploy_with_aws_secrets.py(AWS 凭据可用) - 已配置AWS密钥名称(自动完成)
安全
✅ 永远不要承诺:
.env文件*.log文件config/postgres_config.ini
✅ 可以放心提交:
- AWS 密钥 名字 (例如,“postgres/dev/db”)
- 数据库 主机名 (例如,“mydb.rds.amazonaws.com”)
所有敏感数据都会自动被Git忽略。
高级的
项目结构
postgres_mcp_allaccess/
├── deploy_with_aws_secrets.py # AWS deployment script
├── docker-compose.yml # Container orchestration
├── Dockerfile # Single container (MCP + PgBouncer)
├── docker-entrypoint.sh # Startup script
├── pgbouncer/pgbouncer.ini # Connection pool config
└── src/postgres_mcp_allaccess/
├── server.py # MCP server
├── database.py # Global pool + auto rotation
├── pgbouncer_manager.py # PgBouncer control
└── transports/sse_transport.py # HTTP/SSE transport健康监测
# Health check endpoint
curl http://localhost:3000/health
# Check PgBouncer process
docker exec postgres-mcp-allaccess ps aux | grep pgbouncer
# View all environment variables
docker exec postgres-mcp-allaccess env | grep -E "POSTGRES|AWS|PGBOUNCER"手动刷新密码
# Force refresh from AWS (if auto-rotation fails)
docker-compose restart许可证
Apache许可证2.0 - 详见 许可证 文件中有详细信息。
支持
- 问题: https://github.com/yty-build/postgres_mcp_allaccess/issues 翻译为中文是:“https://github.com/yty-build/postgres_mcp_allaccess/issues(GitHub上的问题页面链接)”。不过,通常在中文语境下,我们不会直接翻译网址,而是说明其含义,即“这是GitHub上yty-build用户或组织的postgres_mcp_allaccess项目的问题跟踪页面”
- 讨论: https://github.com/yty-build/postgres_mcp_allaccess/讨论区(或“讨论版”)
- 安全: 通过(以下方式)私下举报:
