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
1[](https://github.com/subnetmarco/pgmcp/actions/workflows/ci.yml)2[](https://goreportcard.com/report/github.com/subnetmarco/pgmcp)3[](https://opensource.org/licenses/Apache-2.0)45# PGMCP - PostgreSQL Model Context Protocol Server67PGMCP 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.89**Works with**: Cursor, Claude Desktop, VS Code extensions, and any [MCP-compatible client](https://modelcontextprotocol.io/)1011## Quick Start1213PGMCP connects to **your existing PostgreSQL database** and makes it accessible to AI assistants through natural language queries.1415### Prerequisites16- PostgreSQL database (existing database with your schema)17- OpenAI API key (optional, for AI-powered SQL generation)1819### Basic Usage2021```bash22# Set up environment variables23export DATABASE_URL="postgres://user:password@localhost:5432/your-existing-db"24export OPENAI_API_KEY="your-api-key" # Optional2526# Run server (using pre-compiled binary)27./pgmcp-server2829# Test with client in another terminal30./pgmcp-client -ask "What tables do I have?" -format table31./pgmcp-client -ask "Who is the customer that has placed the most orders?" -format table32./pgmcp-client -search "john" -format table33```3435Here is how it works:3637```38๐ค User / AI Assistant39 โ40 โ "Who are the top customers?"41 โผ42โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ43โ Any MCP Client โ44โ โ45โ PGMCP CLI โ Cursor โ Claude Desktop โ VS Code โ ... โ46โ JSON/CSV โ Chat โ AI Assistant โ Editor โ โ47โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ48 โ49 โ Streamable HTTP / MCP Protocol50 โผ51โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ52โ PGMCP Server โ53โ โ54โ ๐ Security ๐ง AI Engine ๐ Streaming โ55โ โข Input Valid โข Schema Cache โข Auto-Pagination โ56โ โข Audit Log โข OpenAI API โข Memory Management โ57โ โข SQL Guard โข Error Recovery โข Connection Pool โ58โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ59 โ60 โ Read-Only SQL Queries61 โผ62โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ63โ Your PostgreSQL Database โ64โ โ65โ Any Schema: E-commerce, Analytics, CRM, etc. โ66โ Tables โข Views โข Indexes โข Functions โ67โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ6869External AI Services:70OpenAI API โข Anthropic โข Local LLMs (Ollama, etc.)7172Key Benefits:73โ Works with ANY PostgreSQL database (no assumptions about schema)74โ No schema modifications required75โ Read-only access (100% safe)76โ Automatic streaming for large results77โ Intelligent query understanding (singular vs plural)78โ Robust error handling (graceful AI failure recovery)79โ PostgreSQL case sensitivity support (mixed-case tables)80โ Production-ready security and performance81โ Universal database compatibility82โ Multiple output formats (table, JSON, CSV)83โ Free-text search across all columns84โ Authentication support85โ Comprehensive testing suite86```8788## Features8990- **Natural Language to SQL**: Ask questions in plain English91- **Automatic Streaming**: Handles large result sets automatically92- **Safe Read-Only Access**: Prevents any write operations93- **Text Search**: Search across all text columns94- **Multiple Output Formats**: Table, JSON, and CSV95- **PostgreSQL Case Sensitivity**: Handles mixed-case table names correctly96- **Universal Compatibility**: Works with any PostgreSQL database9798### Environment Variables99100**Required:**101- `DATABASE_URL`: PostgreSQL connection string to your existing database102103**Optional:**104- `OPENAI_API_KEY`: OpenAI API key for AI-powered SQL generation105- `OPENAI_MODEL`: Model to use (default: "gpt-4o-mini")106- `HTTP_ADDR`: Server address (default: ":8080")107- `HTTP_PATH`: MCP endpoint path (default: "/mcp")108- `AUTH_BEARER`: Bearer token for authentication109110## Installation111112### Download Pre-compiled Binaries1131141. Go to [GitHub Releases](https://github.com/subnetmarco/pgmcp/releases)1152. Download the binary for your platform (Linux, macOS, Windows)1163. Extract and run:117118```bash119# Example for macOS/Linux120tar xzf pgmcp_*.tar.gz121cd pgmcp_*122./pgmcp-server123```124125### Alternative Options126127```bash128# Homebrew (macOS/Linux) - Available after first release129brew tap subnetmarco/homebrew-tap130brew install pgmcp131132# Build from source133go build -o pgmcp-server ./server134go build -o pgmcp-client ./client135```136137### Docker/Kubernetes138139```bash140# Docker141docker run -e DATABASE_URL="postgres://user:pass@host:5432/db" \142 -p 8080:8080 ghcr.io/subnetmarco/pgmcp:latest143144# Kubernetes (see examples/ directory for full manifests)145kubectl create secret generic pgmcp-secret \146 --from-literal=database-url="postgres://user:pass@host:5432/db"147kubectl apply -f examples/k8s/148```149150#### Quick Start151152```bash153# Set up database (optional - works with any existing PostgreSQL database)154export DATABASE_URL="postgres://user:password@localhost:5432/mydb"155psql $DATABASE_URL < schema.sql156157# Run server158export OPENAI_API_KEY="your-api-key"159./pgmcp-server160161# Test with client162./pgmcp-client -ask "Who is the user that places the most orders?" -format table163./pgmcp-client -ask "Show me the top 40 most reviewed items in the marketplace" -format table164```165166### Environment Variables167168**Required:**169- `DATABASE_URL`: PostgreSQL connection string170171**Optional:**172- `OPENAI_API_KEY`: OpenAI API key for SQL generation173- `OPENAI_MODEL`: Model to use (default: "gpt-4o-mini")174- `HTTP_ADDR`: Server address (default: ":8080")175- `HTTP_PATH`: MCP endpoint path (default: "/mcp")176- `AUTH_BEARER`: Bearer token for authentication177178## Usage Examples179180```bash181# Ask questions in natural language182./pgmcp-client -ask "What are the top 5 customers?" -format table183./pgmcp-client -ask "How many orders were placed today?" -format json184185# Search across all text fields186./pgmcp-client -search "john" -format table187188# Multiple questions at once189./pgmcp-client -ask "Show tables" -ask "Count users" -format table190191# Different output formats192./pgmcp-client -ask "Export all data" -format csv -max-rows 1000193```194195## Example Database196197The project includes two schemas:198- **`schema.sql`**: Full Amazon-like marketplace with 5,000+ records199- **`schema_minimal.sql`**: Minimal test schema with mixed-case `"Categories"` table200201**Key features:**202- **Mixed-case table names** (`"Categories"`) for testing case sensitivity203- **Composite primary keys** (`order_items`) for testing AI assumptions204- **Realistic relationships** and data types205206Use your own database:207```bash208export DATABASE_URL="postgres://user:pass@host:5432/your_db"209./pgmcp-server210./pgmcp-client -ask "What tables do I have?"211```212213## AI Error Handling214215When AI generates incorrect SQL, PGMCP handles it gracefully:216217```json218{219 "error": "Column not found in generated query",220 "suggestion": "Try rephrasing your question or ask about specific tables",221 "original_sql": "SELECT non_existent_column FROM table..."222}223```224225Instead of crashing, the system provides helpful feedback and continues operating.226227## MCP Integration228229### Cursor Integration230231```bash232# Start server233export DATABASE_URL="postgres://user:pass@localhost:5432/your_db"234./pgmcp-server235```236237Add to Cursor settings:238```json239{240 "mcp.servers": {241 "pgmcp": {242 "transport": {243 "type": "http",244 "url": "http://localhost:8080/mcp"245 }246 }247 }248}249```250251### Claude Desktop Integration252253Edit `~/.config/claude-desktop/claude_desktop_config.json`:254```json255{256 "mcpServers": {257 "pgmcp": {258 "transport": {259 "type": "http",260 "url": "http://localhost:8080/mcp"261 }262 }263 }264}265```266267## API Tools268269- **`ask`**: Natural language questions โ SQL queries with automatic streaming270- **`search`**: Free-text search across all database text columns271- **`stream`**: Advanced streaming for very large result sets with pagination272273## Safety Features274275- **Read-Only Enforcement**: Blocks write operations (INSERT, UPDATE, DELETE, etc.)276- **Query Timeouts**: Prevents long-running queries277- **Input Validation**: Sanitizes and validates all user input278- **Transaction Isolation**: All queries run in read-only transactions279280## Testing281282```bash283# Unit tests284go test ./server -v285286# Integration tests (requires PostgreSQL)287go test ./server -tags=integration -v288```289290## License291292Apache 2.0 - See LICENSE file for details.293294## Related Projects295296- [Model Context Protocol](https://modelcontextprotocol.io/) - The underlying protocol specification297- [MCP Go SDK](https://github.com/modelcontextprotocol/go-sdk) - Go implementation of MCP298299---300301PGMCP makes your PostgreSQL database accessible to AI assistants through natural language while maintaining security through read-only access controls.302
Full transparency โ inspect the skill content before installing.