PostgreSQL MCP Server
Leverages OpenAI's GPT models to translate natural language questions into optimized SQL queries.
Enables natural language interaction with PostgreSQL databases, automatically generating and executing safe SQL queries with result validation.
Provides Prometheus-format metrics for monitoring server health, query performance, and usage statistics.
Click on "Install Server".
Wait a few minutes for the server to deploy. Once ready, it will show a "Started" state.
In the chat, type
@followed by the MCP server name and your instructions, e.g., "@PostgreSQL MCP ServerWhat are the top 10 products by sales?"
That's it! The server will respond to your query, and you can continue using it as needed.
Here is a step-by-step guide with screenshots.
PostgreSQL MCP Server
A production-grade Model Context Protocol (MCP) server that enables users to interact with PostgreSQL databases using natural language. Built on FastMCP, this server converts natural language questions into safe SQL queries, executes them, and validates the results. Some reference documents:
Python Postgres MCP Requirements Research : https://gemini.google.com/share/c87a73f0969b
SQLGlot Deep Research Proposal : https://gemini.google.com/share/cc5e45c76c8f
Features
Natural Language to SQL: Uses GPT-5.2-mini to convert plain English questions into optimized PostgreSQL queries
Security First: Read-only enforcement, blocking of dangerous functions, SQL injection protection, and query timeout control
Result Validation: AI-based result validation with confidence scoring
Intelligent Schema: Automatic schema caching with TTL-based refresh mechanism
Production Ready: Connection pool management, circuit breakers, rate limiting, and comprehensive metrics collection
MCP Compatible: Supports Claude Desktop and any MCP-compatible client
Related MCP server: PostgreSQL MCP Server
Quick Start
Prerequisites
Python 3.14+
PostgreSQL 12+
OpenAI API Key (for GPT-5.2-mini)
UV package manager (recommended) or pip
Installation
Using UV (Recommended)
# 克隆仓库
git clone <repository-url>
cd pg-mcp
# 安装依赖
uv sync
# 复制环境配置模板
cp .env.example .env
# 编辑 .env 并配置参数
vi .envUsing pip
# 克隆仓库
git clone <repository-url>
cd pg-mcp
# 创建虚拟环境
python -m venv .venv
source .venv/bin/activate # Windows 系统: .venv\Scripts\activate
# 安装依赖
pip install -e .
# 复制环境配置模板
cp .env.example .env
# 编辑 .env 并配置参数
vi .envConfiguration
Edit the .env file to configure your settings:
# 数据库配置
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_NAME=your_database
DATABASE_USER=your_user
DATABASE_PASSWORD=your_password
# OpenAI 配置
OPENAI_API_KEY=sk-your-api-key-here
OPENAI_MODEL=gpt-5.2-mini
# 安全设置(可选,显示默认值)
SECURITY_ALLOW_WRITE_OPERATIONS=false
SECURITY_MAX_ROWS=10000
SECURITY_MAX_EXECUTION_TIME=30For full configuration options, please refer to .env.example.
Running the Server
Standalone Mode
# 使用 UV
uv run python main.py
# 或使用 pip
python main.pyIntegration with Claude Desktop
Add the following configuration to your Claude Desktop MCP settings file:
macOS/Linux: ~/Library/Application Support/Claude/claude_desktop_config.json
Windows: %APPDATA%\Claude\claude_desktop_config.json
{
"mcpServers": {
"postgres": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/pg-mcp",
"run",
"python",
"main.py"
],
"env": {
"DATABASE_HOST": "localhost",
"DATABASE_NAME": "your_database",
"DATABASE_USER": "your_user",
"DATABASE_PASSWORD": "your_password",
"OPENAI_API_KEY": "sk-your-api-key-here"
}
}
}
}For detailed configuration instructions, please refer to Claude Desktop Configuration.
Usage
Example Queries
After connecting via Claude Desktop or another MCP client, you can ask natural language questions:
Simple Query
How many tables are in the database?
→ SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public'
Show me all users
→ SELECT * FROM users LIMIT 10000
What are the column names in the products table?
→ SELECT column_name, data_type FROM information_schema.columns
WHERE table_name = 'products'Analytical Query
What are the top 10 products by sales?
→ SELECT product_name, SUM(quantity * price) as total_sales
FROM orders
GROUP BY product_name
ORDER BY total_sales DESC
LIMIT 10
How many users registered in the last 30 days?
→ SELECT COUNT(*) FROM users
WHERE created_at > CURRENT_DATE - INTERVAL '30 days'SQL-Only Mode
You can also request just the SQL without execution:
Generate SQL to find duplicate emails
Return Type: sql
→ Returns: SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1Return Types
The server supports two return types:
result(default): Executes the query and returns the resultssql: Generates and validates the SQL, but does not execute it
Response Format
Successful Query Response
{
"success": true,
"generated_sql": "SELECT COUNT(*) FROM users",
"data": {
"columns": ["count"],
"rows": [[1523]],
"row_count": 1,
"execution_time": 0.023
},
"confidence": 95,
"tokens_used": 234
}SQL-Only Response
{
"success": true,
"generated_sql": "SELECT * FROM users WHERE created_at > CURRENT_DATE - INTERVAL '30 days'",
"confidence": 90,
"tokens_used": 156
}Error Response
{
"success": false,
"error": {
"code": "SECURITY_VIOLATION",
"message": "Query contains blocked operation: DELETE",
"details": {
"blocked_operation": "DELETE"
}
}
}Architecture
Core Components
┌─────────────────────────────────────────────────────────────┐
│ MCP Server (FastMCP) │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Query Orchestrator │
│ - Coordinates all components │
│ - Manages retry logic │
│ - Handles error recovery │
└─────────────────────────────────────────────────────────────┘
│ │ │
▼ ▼ ▼
┌───────────┐ ┌────────────┐ ┌──────────────┐
│ SQL │ │ SQL │ │ SQL │
│ Generator │────▶│ Validator │────▶│ Executor │
│ (LLM) │ │ (Security) │ │ (Database) │
└───────────┘ └────────────┘ └──────────────┘
│ │
▼ ▼
┌───────────┐ ┌──────────────┐
│ Schema │ │ Result │
│ Cache │ │ Validator │
└───────────┘ │ (LLM) │
└──────────────┘Security Features
Read-only Enforcement: Only SELECT queries are allowed by default
Blocking Dangerous Functions: Blacklist includes dangerous PostgreSQL functions (pg_sleep, file I/O, etc.)
SQL Parsing: Uses sqlglot for accurate SQL structure validation
Injection Protection: Parameterized queries and input sanitization
Resource Limits:
Row limit (default: 10,000)
Query timeout (default: 30 seconds)
Connection pool management
Transaction Isolation: All queries run in a read-only transaction
Resilience Features
Circuit Breaker: Prevents cascading LLM API failures
Rate Limiting: Prevents API quota exhaustion
Retry Logic: Automatic retries for transient failures using exponential backoff
Connection Pooling: Efficient database connection reuse
Schema Caching: TTL-based caching to reduce database metadata queries
Configuration Reference
Database Settings
Variable | Description | Default |
| PostgreSQL Host |
|
| PostgreSQL Port |
|
| Database Name | Required |
| Database User | Required |
| Database Password | Required |
| Min pool connections |
|
| Max pool connections |
|
| Query timeout (seconds) |
|
OpenAI Settings
Variable | Description | Default |
| OpenAI API Key | Required |
| Model to use |
|
| Max tokens per request |
|
| Model temperature |
|
| API timeout (seconds) |
|
Security Settings
Variable | Description | Default |
| Allow INSERT/UPDATE/DELETE |
|
| Comma-separated blacklist | Refer .env.example |
| Max rows per query |
|
| Query timeout (seconds) |
|
Cache Settings
Variable | Description | Default |
| Enable Schema caching |
|
| Schema cache TTL (sec) |
|
| Max cached schemas |
|
Resilience Settings
Variable | Description | Default |
| Max retries |
|
| Initial retry delay (s) |
|
| Exponential backoff |
|
| Failures before trip |
|
| Circuit breaker timeout |
|
Observability Settings
Variable | Description | Default |
| Enable Prometheus metrics |
|
| Metrics HTTP port |
|
| Log level |
|
| Log format (json/text) |
|
Development
Setting up Development Environment
# 安装开发依赖
uv sync --all-extras
# 安装 pre-commit 钩子(可选)
pre-commit installRunning Tests
# 运行所有测试
uv run pytest
# 运行并生成覆盖率报告
uv run pytest --cov=src --cov-report=html
# 运行特定测试类别
uv run pytest tests/unit/ # 仅单元测试
uv run pytest tests/integration/ # 集成测试
uv run pytest tests/e2e/ # 端到端测试
uv run pytest -m integration # 标记为集成的测试Code Quality
# 类型检查
uv run mypy src
# Lint 和格式化
uv run ruff check --fix .
uv run ruff format .
# 运行所有质量检查
uv run pytest --cov=src --cov-fail-under=80
uv run mypy src
uv run ruff check .Project Structure
pg-mcp/
├── src/pg_mcp/
│ ├── cache/ # Schema 缓存
│ ├── config/ # 配置管理
│ ├── db/ # 数据库连接池
│ ├── models/ # 数据模型
│ ├── observability/ # 日志、指标、追踪
│ ├── prompts/ # LLM Prompt 模板
│ ├── resilience/ # 熔断器、限流器
│ ├── services/ # 核心业务逻辑
│ │ ├── orchestrator.py # 查询协调
│ │ ├── sql_generator.py # 基于 LLM 的 SQL 生成
│ │ ├── sql_validator.py # 安全验证
│ │ ├── sql_executor.py # 查询执行
│ │ └── result_validator.py # 结果验证
│ └── server.py # FastMCP 服务器
├── tests/
│ ├── unit/ # 单元测试
│ ├── integration/ # 集成测试
│ └── e2e/ # 端到端测试
├── fixtures/ # 测试数据库 fixture
├── .env.example # 环境模板
├── pyproject.toml # 项目配置
└── main.py # 入口点Docker Deployment
Building the Image
docker build -t pg-mcp:latest .Running the Container
docker run -d \
--name pg-mcp \
-e DATABASE_HOST=your-db-host \
-e DATABASE_NAME=your-db \
-e DATABASE_USER=your-user \
-e DATABASE_PASSWORD=your-password \
-e OPENAI_API_KEY=sk-your-key \
-p 9090:9090 \
pg-mcp:latestDocker Compose
# 启动所有服务(PostgreSQL + pg-mcp)
docker-compose up -d
# 查看日志
docker-compose logs -f pg-mcp
# 停止服务
docker-compose downRefer to docker-compose.yml for detailed configuration.
Monitoring
Metrics
The server exposes Prometheus metrics on port 9090 (configurable):
curl http://localhost:9090/metricsAvailable Metrics:
pg_mcp_queries_total- Total queries processedpg_mcp_query_duration_seconds- Query execution time histogrampg_mcp_sql_generation_duration_seconds- SQL generation timepg_mcp_sql_validation_failures_total- Number of validation failurespg_mcp_database_errors_total- Number of database errorspg_mcp_llm_tokens_used_total- Total LLM tokens used
Logs
Structured JSON logs (or text format) are output to stdout:
{
"timestamp": "2025-12-20T10:30:00.123Z",
"level": "INFO",
"message": "Query executed successfully",
"database": "mydb",
"execution_time": 0.023,
"row_count": 42
}Troubleshooting
Common Issues
Connection Refused
Error: Connection to database failedSolution: Verify that PostgreSQL is running and credentials are correct:
psql -h $DATABASE_HOST -U $DATABASE_USER -d $DATABASE_NAMEOpenAI API Error
Error: OpenAI API request failedSolution:
Check if the API key is valid and has credits
Verify network connectivity
If requests time out, check the
OPENAI_TIMEOUTsetting
Query Timeout
Error: Query execution timeout exceededSolution:
Increase
SECURITY_MAX_EXECUTION_TIMEOptimize the database (add indexes, VACUUM)
Simplify the query or add filter conditions
Schema Cache Issues
Error: Schema not found in cacheSolution:
Restart the server to reload the schema
Verify the database user has schema read permissions
Check if
CACHE_ENABLEDis set totrue
Debug Mode
Enable debug logging:
export OBSERVABILITY_LOG_LEVEL=DEBUG
uv run python main.pyClaude Desktop Configuration
macOS/Linux Configuration
Edit ~/Library/Application Support/Claude/claude_desktop_config.json:
{
"mcpServers": {
"postgres": {
"command": "uv",
"args": [
"--directory",
"/Users/yourname/projects/pg-mcp",
"run",
"python",
"main.py"
],
"env": {
"DATABASE_HOST": "localhost",
"DATABASE_PORT": "5432",
"DATABASE_NAME": "mydb",
"DATABASE_USER": "postgres",
"DATABASE_PASSWORD": "your-password",
"OPENAI_API_KEY": "sk-your-api-key-here",
"OPENAI_MODEL": "gpt-5.2-mini",
"SECURITY_MAX_ROWS": "10000",
"CACHE_ENABLED": "true",
"OBSERVABILITY_LOG_LEVEL": "INFO"
}
}
}
}Windows Configuration
Edit %APPDATA%\Claude\claude_desktop_config.json:
{
"mcpServers": {
"postgres": {
"command": "uv",
"args": [
"--directory",
"C:\\Users\\YourName\\projects\\pg-mcp",
"run",
"python",
"main.py"
],
"env": {
"DATABASE_HOST": "localhost",
"DATABASE_NAME": "mydb",
"DATABASE_USER": "postgres",
"DATABASE_PASSWORD": "your-password",
"OPENAI_API_KEY": "sk-your-api-key-here"
}
}
}
}Using Python Virtualenv
If not using UV, configure Python directly:
{
"mcpServers": {
"postgres": {
"command": "/absolute/path/to/pg-mcp/.venv/bin/python",
"args": ["main.py"],
"cwd": "/absolute/path/to/pg-mcp",
"env": {
"DATABASE_HOST": "localhost",
...
}
}
}
}Restarting Claude Desktop
After editing the configuration:
Quit Claude Desktop completely
Restart Claude Desktop
The PostgreSQL MCP server will be available
Security Considerations
Production Deployment
Use a Read-Only Database User: Create a dedicated PostgreSQL user with only SELECT permissions:
CREATE USER pg_mcp_readonly WITH PASSWORD 'secure-password';
GRANT CONNECT ON DATABASE your_database TO pg_mcp_readonly;
GRANT USAGE ON SCHEMA public TO pg_mcp_readonly;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO pg_mcp_readonly;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT ON TABLES TO pg_mcp_readonly;Protect API Keys: Use environment variables or a secret management system; never commit to version control
Network Isolation: Run the server in an isolated network, restricting database access by IP
Monitor Usage: Enable metrics and set up alerts for anomalous patterns
Rate Limiting: Configure appropriate rate limiting parameters to prevent abuse
Log Sanitization: Sensitive data is automatically filtered from logs
License
[Your License Information]
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
Support
For questions and issues:
GitHub Issues: [repository-url]/issues
Documentation: Check the
specs/w5/directory for detailed design documents
Acknowledgments
Maintenance
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Tools
- addB
Related MCP Servers
- -licenseNot gradedqualityNot gradedmaintenanceEnables secure read-only interactions with PostgreSQL databases through natural language. Provides database inspection, table listing, and SQL query execution with built-in security validation.
- AlicenseAqualityAmaintenanceEnables read-only interaction with PostgreSQL databases through natural language queries, supporting dynamic connections and secure query validation.31952MIT
- FlicenseNot gradedqualityDmaintenanceEnables AI assistants to interact with PostgreSQL databases through natural language queries, schema inspection, and safe SQL execution.91
- AlicenseNot gradedqualityDmaintenanceEnables natural language querying of PostgreSQL databases with intelligent SQL generation using LLMs.1Apache 2.0
Related MCP Connectors
Query PostgreSQL databases in plain English — LLM-generated, safety-validated SQL.
Analytical memory for AI agents: a real Postgres queried in plain English over MCP. One command.
Comprehensive PostgreSQL documentation and best practices, including ecosystem tools
Latest Blog Posts
- Who's Calling? MCP Hosts Are an Identity Blind Spot (And the Spec Knows It)By Om-Shree-0709 on .mcpAgent IdentityOAuth 2.1
- Your AI Chatbot Just Exposed Your CEO's Salary to an InternBy Om-Shree-0709 on .Agent IdentityMCP SecurityOAuth Delegation
- Why MCP Servers Need Execution Sandboxing (And Why Your Current Stack Isn't Enough)By Om-Shree-0709 on .Agentic AiPrompt InjectionWebAssembly
MCP directory API
We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/lastfore/pg-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server