
PostgreSQL MCP
STDIOPostgreSQL MCP server for AI agents to discover, query, and understand databases.
PostgreSQL MCP server for AI agents to discover, query, and understand databases.
A Model Context Protocol (MCP) server for PostgreSQL databases with enhanced capabilities for AI agents.
More info on the pg-mcp project here:
PG-MCP is a server implementation of the Model Context Protocol for PostgreSQL databases. It provides a comprehensive API for AI agents to discover, connect to, query, and understand PostgreSQL databases through MCP's resource-oriented architecture.
This implementation builds upon and extends the reference Postgres MCP implementation with several key enhancements:
Built-in contextual information for PostgreSQL extensions like:
Additional extensions can be easily added via YAML config files.
# Clone the repository git clone https://github.com/stuzero/pg-mcp-server.git cd pg-mcp-server # Build and run with Docker Compose docker-compose up -d
# Clone the repository git clone https://github.com/stuzero/pg-mcp-server.git cd pg-mcp-server # Install dependencies and create a virtual environment ( .venv ) uv sync # Activate the virtual environment source .venv/bin/activate # On Windows: .venv\Scripts\activate # Run the server python -m server.app
The repository includes test scripts to verify server functionality:
# Basic server functionality test python test.py "postgresql://username:password@hostname:port/database" # Claude-powered natural language to SQL conversion python example-clients/claude_cli.py "Show me the top 5 customers by total sales"
The claude_cli.py
script requires environment variables:
# .env file
DATABASE_URL=postgresql://username:password@hostname:port/database
ANTHROPIC_API_KEY=your-anthropic-api-key
PG_MCP_URL=http://localhost:8000/sse
Example prompt for use with agents:
Use the PostgreSQL MCP server to analyze the database.
Available tools:
- connect: Register a database connection string and get a connection ID
- disconnect: Close a database connection
- pg_query: Execute SQL queries using a connection ID
- pg_explain: Get query execution plans
You can explore schema resources via:
pgmcp://{conn_id}/schemas
pgmcp://{conn_id}/schemas/{schema}/tables
pgmcp://{conn_id}/schemas/{schema}/tables/{table}/columns
A comprehensive database description is available at this resource:
pgmcp://{conn_id}/
This server is built on:
Contributions are welcome! Areas for expansion: