pgmcp
An MCP server to query any Postgres database in natural language.
claude mcp add --transport stdio subnetmarco-pgmcp docker run -i -p 8080:8080 ghcr.io/subnetmarco/pgmcp:latest \ --env HTTP_ADDR="Server address (default: :8080)" \ --env HTTP_PATH="MCP endpoint path (default: /mcp)" \ --env AUTH_BEARER="Bearer token for authentication" \ --env DATABASE_URL="PostgreSQL connection string, e.g. postgres://user:pass@host:5432/db" \ --env OPENAI_MODEL="Model to use (default: gpt-4o-mini)" \ --env OPENAI_API_KEY="Optional OpenAI API key for AI-powered SQL generation"
How to use
PGMCP exposes a read-only MCP server that translates natural language queries into SQL against your PostgreSQL database. It connects to your existing database via a connection string and uses an AI model to generate and refine SQL queries, streaming results back to clients in multiple formats (table, JSON, CSV). You can access it through any MCP-compatible client or integration (Cursor, Claude Desktop, VS Code extensions, etc.). To start, run the server with a connected PostgreSQL database and an optional OpenAI API key for enhanced SQL generation. Then use the included client tools or your MCP client to ask questions like: What are the top customers by revenue, or Show me the orders placed in the last 7 days. The system handles read-only access, streaming, error recovery, and supports mixed-case PostgreSQL identifiers.
How to install
Prerequisites:
- PostgreSQL database (existing database with your schema)
- Docker installed (for the recommended container-based setup) or go compiler if you build from source
- Optional: OpenAI API key for AI-powered SQL generation
Installation (Docker-based):
- Ensure you have Docker installed and running.
- Run the container with required environment variables and port mapping:
docker run -i \
-p 8080:8080 \
-e DATABASE_URL="postgres://user:pass@host:5432/db" \
-e OPENAI_API_KEY="your-api-key" \
-e OPENAI_MODEL="gpt-4o-mini" \
-e HTTP_ADDR=":8080" \
-e HTTP_PATH="/mcp" \
-e AUTH_BEARER="your-secret" \
ghcr.io/subnetmarco/pgmcp:latest
If you prefer building from source (Go):
- Install Go (1.18+).
- Clone the repository and build:
go build -o pgmcp-server ./server
- Run with a PostgreSQL connection string and optional API key:
export DATABASE_URL="postgres://user:pass@host:5432/db"
export OPENAI_API_KEY="your-api-key" # Optional
./pgmcp-server
Alternative installation (pre-compiled binaries): download the appropriate binary from the GitHub Releases, extract, and run the server binary directly.
Additional notes
Tips:
- Use a read-only PostgreSQL user to maximize safety.
- If you encounter SQL errors from AI generation, enable verbose logging and review AI-generated SQL for correctness; the system is designed to gracefully recover and retry.
- Ensure the DATABASE_URL is accessible from the environment where the server runs (network access, correct host, and permissions).
- The default HTTP path is /mcp and default port is 8080; adjust HTTP_ADDR and HTTP_PATH as needed for your deployment.
- When using Docker, you can map a different host port if 8080 is in use, e.g., -p 8081:8080 and set HTTP_ADDR=":8081".
- If you run behind a reverse proxy, configure TLS at the proxy and forward to the MCP endpoint.
Related MCP Servers
decipher-research-agent
Turn topics, links, and files into AI-generated research notebooks — summarize, explore, and ask anything.
MCP-PostgreSQL-Ops
🔍Professional MCP server for PostgreSQL operations & monitoring: 30+ extension-independent tools for performance analysis, table bloat detection, autovacuum monitoring, schema introspection, and database management. Supports PostgreSQL 12-17.
hyperterse
The MCP framework. Connect your data to your agents.
okta
The Okta MCP Server is a groundbreaking tool built by the team at Fctr that enables AI models to interact directly with your Okta environment using the Model Context Protocol (MCP). Built specifically for IAM engineers, security teams, and Okta administrators, it implements the MCP specification to help work with Okta enitities
zerodha
Zerodha MCP Server & Client - AI Agent (w/Agno & w/Google ADK)
Common_Chronicle
Common Chronicle turns messy context into structured, sourced timelines.