PG MCP
PG-MCP is an HTTP server implementation that enables AI systems to interact with PostgreSQL databases via MCP, providing tools for querying, connecting to multiple databases, and exploring schema resources. The system enriches context by extracting t
What is PG MCP?
PG MCP is a Model Context Protocol (MCP) server that allows AI assistants like Claude, Cursor, and VS Code to pg-mcp is an http server implementation that enables ai systems to interact with postgresql databases via mcp, providing tools for querying, connecting to multiple databases, and exploring schema reso...
PG-MCP is an HTTP server implementation that enables AI systems to interact with PostgreSQL databases via MCP, providing tools for querying, connecting to multiple databases, and exploring schema resources. The system enriches context by extracting t
This server falls under the Databases category on MCPgee, the world's largest MCP server directory with 33,000+ servers.
Features
- PG-MCP is an HTTP server implementation that enables AI syst
Use Cases
Maintainer
Works with
Installation
NPM
npx -y pg-mcp-serverManual Installation
npx -y pg-mcp-serverConfiguration
Configuration Details
claude_desktop_config.json
Performance
Response Metrics
Resource Usage
How to Set Up and Use PG MCP
PG-MCP is an HTTP-based MCP server that lets AI systems query PostgreSQL databases using natural language, automatically translating questions into safe, SELECT-only SQL via OpenAI. It auto-discovers the full schema of connected databases on startup — including tables, views, columns, indexes, foreign keys, and enums — so AI assistants can answer questions about your data without requiring users to know SQL syntax. Database teams and analysts use it to enable conversational data access across multiple PostgreSQL databases while enforcing query safety through a built-in SQL validator.
Prerequisites
- Python 3.10+ with pip installed
- One or more PostgreSQL databases accessible via connection string
- An OpenAI API key (OPENAI_API_KEY) for natural language to SQL translation
- An MCP-capable client such as Claude Desktop or Claude Code
Clone the repository and install
Clone the pg-mcp repository and install it in editable mode using pip.
git clone https://github.com/awaketai/postgres-mcp
cd postgres-mcp
pip install -e .Set required environment variables
Configure the database connection string and OpenAI API key. PG_MCP_DATABASES accepts a comma-separated list of PostgreSQL DSNs for multi-database support.
export PG_MCP_DATABASES="postgresql://user:password@localhost:5432/mydb"
export OPENAI_API_KEY="sk-..."
# Optional settings
export OPENAI_MODEL="gpt-4o"
export PG_MCP_MAX_ROWS=100
export PG_MCP_QUERY_TIMEOUT=30Start the MCP server
Start the pg-mcp server. It will auto-discover the schema for all configured databases on startup.
pg-mcpAdd to Claude Desktop configuration
Configure Claude Desktop to use pg-mcp with your database and OpenAI credentials.
{
"mcpServers": {
"pg-mcp": {
"command": "pg-mcp",
"env": {
"PG_MCP_DATABASES": "postgresql://user:password@localhost:5432/mydb",
"OPENAI_API_KEY": "sk-...",
"PG_MCP_MAX_ROWS": "100"
}
}
}
}Refresh the schema cache if needed
If you make schema changes to your database, use the refresh_schema tool to reload the schema cache without restarting the server.
PG MCP Examples
Client configuration
Claude Desktop JSON configuration for pg-mcp connecting to a local PostgreSQL database.
{
"mcpServers": {
"pg-mcp": {
"command": "pg-mcp",
"env": {
"PG_MCP_DATABASES": "postgresql://user:password@localhost:5432/mydb",
"OPENAI_API_KEY": "sk-...",
"OPENAI_MODEL": "gpt-4o",
"PG_MCP_MAX_ROWS": "100",
"PG_MCP_QUERY_TIMEOUT": "30"
}
}
}
}Prompts to try
Example natural-language data queries once pg-mcp is connected to your AI assistant.
- "How many users registered in the last 30 days?"
- "Show me the top 10 products by revenue this quarter"
- "List all databases connected to this MCP server"
- "Describe the schema of the orders table including indexes and foreign keys"
- "Which customers have placed more than 5 orders in the past year?"Troubleshooting PG MCP
Connection to PostgreSQL fails on startup
Verify that PG_MCP_DATABASES contains a valid PostgreSQL DSN including host, port, database name, user, and password. Test the connection directly with psql or a database client before running pg-mcp.
OpenAI API errors when running natural language queries
Confirm that OPENAI_API_KEY is set correctly and that the selected OPENAI_MODEL (default: gpt-4o) is available on your OpenAI account. Check your usage limits and billing status if requests fail consistently.
Query results are incomplete or truncated
The default PG_MCP_MAX_ROWS is 100. Increase this value (up to 1000) by setting PG_MCP_MAX_ROWS in your environment. For time-sensitive queries, also adjust PG_MCP_QUERY_TIMEOUT beyond the 30-second default.
Frequently Asked Questions about PG MCP
What is PG MCP?
PG MCP is a Model Context Protocol (MCP) server that pg-mcp is an http server implementation that enables ai systems to interact with postgresql databases via mcp, providing tools for querying, connecting to multiple databases, and exploring schema resources. the system enriches context by extracting t It connects AI assistants to external tools and data sources through a standardized interface.
How do I install PG MCP?
Install via npm with the command: npx -y pg-mcp-server. Then add the server configuration to your AI client's JSON config file (e.g., claude_desktop_config.json or .cursor/mcp.json).
Which AI clients work with PG MCP?
PG MCP works with all major MCP-compatible AI clients including Claude Desktop, Claude Code, Cursor, VS Code (GitHub Copilot), Windsurf, and Cline.
Is PG MCP free to use?
Yes, PG MCP is open source and available under the MIT license. You can use it freely in both personal and commercial projects.
PG MCP Alternatives — Similar Databases Servers
Looking for alternatives to PG MCP? Here are other popular databases servers you can use with Claude, Cursor, and VS Code.
Excelize
★ 20.6kGo language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
MCP Toolbox for Databases
★ 15.3kOpen source MCP server specializing in easy, fast, and secure tools for Databases.
DBHub
★ 2.8kA universal database gateway MCP server that enables AI assistants to connect to and query multiple databases (PostgreSQL, MySQL, MariaDB, SQL Server, SQLite) with support for schema exploration, SQL execution, and secure connections via SSH tunnels.
Tabularis
★ 2.1kA lightweight, cross-platform database client for developers. Supports MySQL, PostgreSQL and SQLite. Hackable with plugins. Built for speed, security, and aesthetics.
Postgres AI Guide
★ 1.7kMCP server and Claude plugin for Postgres skills and documentation. Helps AI coding tools generate better PostgreSQL code.
Anyquery
★ 1.7k🏎️ 🏠 ☁️ - Query more than 40 apps with one binary using SQL. It can also connect to your PostgreSQL, MySQL, or SQLite compatible database. Local-first and private by design.
Browse More Databases MCP Servers
Explore all databases servers available in the MCPgee directory. Each server includes setup guides for Claude, Cursor, and VS Code.
Set Up PG MCP in Your Editor
Choose your AI client for step-by-step setup instructions.
Quick Config Preview
Add this to your claude_desktop_config.json or .cursor/mcp.json
Ready to use PG MCP?
Browse our complete directory of 33,000+ MCP servers, read setup guides for your editor, and start building with the Model Context Protocol.