PGMCP connects AI assistants to any PostgreSQL database through natural language queries. Ask questions in plain English and get structured SQL results with automatic streaming and robust error handling. Works with: Cursor, Claude Desktop, VS Code extensions, and any MCP-compatible client PGMCP connects to your existing PostgreSQL database and makes it accessible to AI assistants through natural l
Add this skill
npx mdskills install subnetmarco/pgmcpWell-documented PostgreSQL MCP server with natural language querying, streaming, and robust safety controls
PGMCP connects AI assistants to any PostgreSQL database through natural language queries. Ask questions in plain English and get structured SQL results with automatic streaming and robust error handling.
Works with: Cursor, Claude Desktop, VS Code extensions, and any MCP-compatible client
PGMCP connects to your existing PostgreSQL database and makes it accessible to AI assistants through natural language queries.
# Set up environment variables
export DATABASE_URL="postgres://user:password@localhost:5432/your-existing-db"
export OPENAI_API_KEY="your-api-key" # Optional
# Run server (using pre-compiled binary)
./pgmcp-server
# Test with client in another terminal
./pgmcp-client -ask "What tables do I have?" -format table
./pgmcp-client -ask "Who is the customer that has placed the most orders?" -format table
./pgmcp-client -search "john" -format table
Here is how it works:
๐ค User / AI Assistant
โ
โ "Who are the top customers?"
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Any MCP Client โ
โ โ
โ PGMCP CLI โ Cursor โ Claude Desktop โ VS Code โ ... โ
โ JSON/CSV โ Chat โ AI Assistant โ Editor โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โ Streamable HTTP / MCP Protocol
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PGMCP Server โ
โ โ
โ ๐ Security ๐ง AI Engine ๐ Streaming โ
โ โข Input Valid โข Schema Cache โข Auto-Pagination โ
โ โข Audit Log โข OpenAI API โข Memory Management โ
โ โข SQL Guard โข Error Recovery โข Connection Pool โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โ Read-Only SQL Queries
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your PostgreSQL Database โ
โ โ
โ Any Schema: E-commerce, Analytics, CRM, etc. โ
โ Tables โข Views โข Indexes โข Functions โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
External AI Services:
OpenAI API โข Anthropic โข Local LLMs (Ollama, etc.)
Key Benefits:
โ
Works with ANY PostgreSQL database (no assumptions about schema)
โ
No schema modifications required
โ
Read-only access (100% safe)
โ
Automatic streaming for large results
โ
Intelligent query understanding (singular vs plural)
โ
Robust error handling (graceful AI failure recovery)
โ
PostgreSQL case sensitivity support (mixed-case tables)
โ
Production-ready security and performance
โ
Universal database compatibility
โ
Multiple output formats (table, JSON, CSV)
โ
Free-text search across all columns
โ
Authentication support
โ
Comprehensive testing suite
Required:
DATABASE_URL: PostgreSQL connection string to your existing databaseOptional:
OPENAI_API_KEY: OpenAI API key for AI-powered SQL generationOPENAI_MODEL: Model to use (default: "gpt-4o-mini")HTTP_ADDR: Server address (default: ":8080")HTTP_PATH: MCP endpoint path (default: "/mcp")AUTH_BEARER: Bearer token for authentication# Example for macOS/Linux
tar xzf pgmcp_*.tar.gz
cd pgmcp_*
./pgmcp-server
# Homebrew (macOS/Linux) - Available after first release
brew tap subnetmarco/homebrew-tap
brew install pgmcp
# Build from source
go build -o pgmcp-server ./server
go build -o pgmcp-client ./client
# Docker
docker run -e DATABASE_URL="postgres://user:pass@host:5432/db" \
-p 8080:8080 ghcr.io/subnetmarco/pgmcp:latest
# Kubernetes (see examples/ directory for full manifests)
kubectl create secret generic pgmcp-secret \
--from-literal=database-url="postgres://user:pass@host:5432/db"
kubectl apply -f examples/k8s/
# Set up database (optional - works with any existing PostgreSQL database)
export DATABASE_URL="postgres://user:password@localhost:5432/mydb"
psql $DATABASE_URL < schema.sql
# Run server
export OPENAI_API_KEY="your-api-key"
./pgmcp-server
# Test with client
./pgmcp-client -ask "Who is the user that places the most orders?" -format table
./pgmcp-client -ask "Show me the top 40 most reviewed items in the marketplace" -format table
Required:
DATABASE_URL: PostgreSQL connection stringOptional:
OPENAI_API_KEY: OpenAI API key for SQL generationOPENAI_MODEL: Model to use (default: "gpt-4o-mini")HTTP_ADDR: Server address (default: ":8080")HTTP_PATH: MCP endpoint path (default: "/mcp")AUTH_BEARER: Bearer token for authentication# Ask questions in natural language
./pgmcp-client -ask "What are the top 5 customers?" -format table
./pgmcp-client -ask "How many orders were placed today?" -format json
# Search across all text fields
./pgmcp-client -search "john" -format table
# Multiple questions at once
./pgmcp-client -ask "Show tables" -ask "Count users" -format table
# Different output formats
./pgmcp-client -ask "Export all data" -format csv -max-rows 1000
The project includes two schemas:
schema.sql: Full Amazon-like marketplace with 5,000+ recordsschema_minimal.sql: Minimal test schema with mixed-case "Categories" tableKey features:
"Categories") for testing case sensitivityorder_items) for testing AI assumptionsUse your own database:
export DATABASE_URL="postgres://user:pass@host:5432/your_db"
./pgmcp-server
./pgmcp-client -ask "What tables do I have?"
When AI generates incorrect SQL, PGMCP handles it gracefully:
{
"error": "Column not found in generated query",
"suggestion": "Try rephrasing your question or ask about specific tables",
"original_sql": "SELECT non_existent_column FROM table..."
}
Instead of crashing, the system provides helpful feedback and continues operating.
# Start server
export DATABASE_URL="postgres://user:pass@localhost:5432/your_db"
./pgmcp-server
Add to Cursor settings:
{
"mcp.servers": {
"pgmcp": {
"transport": {
"type": "http",
"url": "http://localhost:8080/mcp"
}
}
}
}
Edit ~/.config/claude-desktop/claude_desktop_config.json:
{
"mcpServers": {
"pgmcp": {
"transport": {
"type": "http",
"url": "http://localhost:8080/mcp"
}
}
}
}
ask: Natural language questions โ SQL queries with automatic streamingsearch: Free-text search across all database text columnsstream: Advanced streaming for very large result sets with pagination# Unit tests
go test ./server -v
# Integration tests (requires PostgreSQL)
go test ./server -tags=integration -v
Apache 2.0 - See LICENSE file for details.
PGMCP makes your PostgreSQL database accessible to AI assistants through natural language while maintaining security through read-only access controls.
Install via CLI
npx mdskills install subnetmarco/pgmcpPGMCP - PostgreSQL Model Context Protocol Server is a free, open-source AI agent skill. PGMCP connects AI assistants to any PostgreSQL database through natural language queries. Ask questions in plain English and get structured SQL results with automatic streaming and robust error handling. Works with: Cursor, Claude Desktop, VS Code extensions, and any MCP-compatible client PGMCP connects to your existing PostgreSQL database and makes it accessible to AI assistants through natural l
Install PGMCP - PostgreSQL Model Context Protocol Server with a single command:
npx mdskills install subnetmarco/pgmcpThis downloads the skill files into your project and your AI agent picks them up automatically.
PGMCP - PostgreSQL Model Context Protocol Server works with Claude Code, Claude Desktop, Cursor, Vscode Copilot, Windsurf, Continue Dev, Codex, Gemini Cli, Amp, Roo Code, Goose, Opencode, Trae, Qodo, Command Code. Skills use the open SKILL.md format which is compatible with any AI coding agent that reads markdown instructions.