pg-mcp
Uses OpenAI's language models to translate natural language questions into valid SQL queries for PostgreSQL databases, with safety validation and schema awareness.
Enables natural language querying of PostgreSQL databases with schema auto-discovery, read-only transaction execution, and multi-database support.
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., "@pg-mcpshow me the top 5 products by revenue this month"
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.
pg-mcp
Natural language PostgreSQL query service via MCP (Model Context Protocol). Uses OpenAI to translate natural language into SQL, validates and executes it safely, and returns results to any MCP-compatible client.
Features
Natural language to SQL via OpenAI
Auto-discovers database schema on startup (tables, views, columns, indexes, foreign keys, enums)
Schema cache with optional disk persistence
SQL safety validation (SELECT-only, dangerous function blocking, LIMIT enforcement)
Read-only transaction execution with configurable timeout
Result validation with automatic retry on empty results
Supports multiple databases
Requirements
Python 3.11+
PostgreSQL
OpenAI API key
Installation
pip install -e .For development:
pip install -e ".[dev]"Configuration
Copy .env.example to .env and fill in your values:
cp .env.example .envVariable | Required | Default | Description |
| Yes | — | Comma-separated PostgreSQL connection strings |
| No | First DSN | Default database for queries |
| Yes | — | OpenAI API key |
| No |
| Model to use for SQL generation |
| No | OpenAI default | Custom endpoint (Azure, proxy, etc.) |
| No |
| Query timeout in seconds |
| No |
| Max rows returned (1-1000) |
| No | None | Path for persistent schema cache |
| No |
|
|
Multiple databases example:
PG_MCP_DATABASES=postgresql://user:pass@host1:5432/db1,postgresql://user:pass@host2:5432/db2Usage with Claude Desktop
Add to your claude_desktop_config.json:
{
"mcpServers": {
"pg-mcp": {
"command": "pg-mcp",
"args": [],
"env": {
"PG_MCP_DATABASES": "postgresql://user:pass@localhost:5432/mydb",
"OPENAI_API_KEY": "sk-...",
"PG_MCP_SCHEMA_CACHE_PATH": "~/.cache/pg-mcp/schema.json"
}
}
}
}MCP Tools
query
Query a database using natural language.
{
"question": "What are the top 10 customers by sales last month?",
"database": "mydb",
"return_sql": false,
"max_rows": 100
}return_sql: true— returns the generated SQL without executingreturn_sql: false(default) — executes and returns results
list_databases
List all accessible databases with summary info.
describe_database
Get detailed schema info for a database.
{
"database": "mydb",
"schema": "public",
"pattern": "user"
}refresh_schema
Reload schema cache from the database.
{
"database": "mydb"
}Omit database to refresh all databases.
Testing with Fixture Databases
The fixtures/ directory provides three pre-built databases for local testing.
Setup
# From project root, create & seed all fixture databases
cd fixtures
make all
# Or create individual ones
make small # pg_mcp_test_small — bookshelf (4 tables, ~50 rows)
make medium # pg_mcp_test_medium — ecommerce (12 tables, ~500 rows)
make large # pg_mcp_test_large — enterprise (28 tables, ~5000 rows)Override PostgreSQL credentials via environment (defaults: root / admin123 @ localhost:5432):
make all PGUSER=myuser PGPASSWORD=mypass PGHOST=127.0.0.1Configure .env
Point PG_MCP_DATABASES to the fixture databases:
PG_MCP_DATABASES=postgresql://root:admin123@localhost:5432/pg_mcp_test_small,postgresql://root:admin123@localhost:5432/pg_mcp_test_medium,postgresql://root:admin123@localhost:5432/pg_mcp_test_largeTest with MCP Inspector
Use the built-in MCP inspector for interactive testing:
cd /path/to/postgres-mcp
fastmcp dev src/pg_mcp/server.py:mcpThis opens a browser UI where you can call tools like query, list_databases, and describe_database directly.
Test with Claude Desktop
Add to claude_desktop_config.json:
{
"mcpServers": {
"pg-mcp": {
"command": "python3",
"args": ["-m", "pg_mcp.server"],
"cwd": "/path/to/postgres-mcp"
}
}
}Note: When using
cwd, the server reads.envfrom that directory. You can also pass env vars via the"env"key instead.
Test Queries
Once connected, try these natural language queries against the fixture databases:
Database | Example Query |
| "列出所有评分高于4分的图书" |
| "哪个国家的作者最多" |
| "上个月销量前10的商品" |
| "每个用户的总消费金额" |
| "各部门员工数量和平均薪资" |
| "今年收入最高的前5个销售代表" |
Cleanup
cd fixtures
make drop-all # Remove all fixture databases
make refresh-all # Drop + recreate + re-seedRunning Tests
python3 -m pytest tests/ -vHow It Works
User question
→ Schema context assembled from cache
→ OpenAI generates SQL
→ Safety validation (SELECT-only, no dangerous functions, LIMIT injection)
→ Execute in read-only transaction with timeout
→ Optional result validation on empty results
→ Return SQL or query resultsSecurity
Generated SQL is validated to be SELECT-only (including CTE checks)
40+ dangerous PostgreSQL functions are blocked (session control, advisory locks, dblink, filesystem access, sequence mutation, etc.)
All queries execute in read-only transactions
Automatic LIMIT enforcement prevents large result sets
Query timeout prevents long-running queries
Connection passwords never appear in logs or API responses
This server cannot be installed
Resources
Unclaimed servers have limited discoverability.
Looking for Admin?
If you are the server author, to access and configure the admin panel.
Latest Blog Posts
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/awaketai/postgres-mcp'
If you have feedback or need assistance with the MCP directory API, please join our Discord server