PostgreSQL MCP
by JaviMaligno
Query and explore PostgreSQL databases through your AI assistant
database Python Intermediate Self-hostable No API key Verified
π
Updated: 3w ago
Description
PostgreSQL MCP gives your AI assistant direct, read-oriented access to PostgreSQL databases. Instead of context-switching between your editor and a database client, you can ask your AI to inspect table structures, run queries, and explain execution plans β all within the same conversation where you are writing code. This tight integration between database exploration and code generation dramatically speeds up data-layer development.
The server exposes a focused set of tools designed around the most common database tasks during development. You get schema introspection through `list_tables`, `list_schemas`, and `describe_table`, which let the AI understand your data model before writing any queries. The `query` tool executes SQL and returns formatted results, while `explain_query` shows the execution plan so the AI can help you optimize slow queries with concrete data rather than guesswork.
Security is handled through the connection string you provide at startup. The server connects to exactly one database with whatever permissions that connection string grants. For development workflows, this typically means connecting to a local or staging database. There is no write-access restriction at the MCP level, so use a read-only database user if you want to ensure the AI cannot modify data β a best practice that takes just one SQL command to set up.
β Best for
Backend developers working with PostgreSQL who want their AI to understand and query their actual database schema during development
βοΈ Skip if
You use MySQL, SQLite, or another non-PostgreSQL database β look for database-specific MCP servers or a generic SQL MCP
π‘ Use cases
- Exploring unfamiliar database schemas to understand data relationships before writing application code
- Writing and iterating on complex SQL queries with AI assistance, including execution plan analysis
- Generating ORM models, migrations, or API endpoints based on actual database structure
π Pros
- β Direct schema introspection lets the AI understand your data model without manual explanation
- β Query execution plan analysis helps optimize slow queries with concrete data
- β Simple connection string configuration β point it at any accessible PostgreSQL instance
π Cons
- β PostgreSQL only β not compatible with MySQL, SQLite, or other databases
- β No built-in read-only enforcement β you need to use a read-only database user for safety
π§ Exposed tools (5 tools)
| Tool | Category | Description |
|---|---|---|
| query | query | Execute a SQL query against the connected PostgreSQL database and return the results |
| explain_query | query | Show the execution plan for a SQL query to analyze performance and optimization opportunities |
| list_tables | schema inspection | List all tables in the current database or a specific schema |
| describe_table | schema inspection | Show the column definitions, types, and constraints for a given table |
| list_schemas | schema inspection | List all available schemas in the connected PostgreSQL database |
β‘ Installation
Prerequisites:
- β’ python
claude mcp add postgres -- npx @anthropic-ai/postgres-mcp@latest postgresql://localhost:5432/mydbπ‘ Tips & tricks
Always connect with a read-only database user in production-like environments. Create one with: `CREATE ROLE readonly LOGIN PASSWORD 'pass'; GRANT CONNECT ON DATABASE mydb TO readonly; GRANT USAGE ON SCHEMA public TO readonly; GRANT SELECT ON ALL TABLES IN SCHEMA public TO readonly;`. For local development, the default superuser is fine. When asking the AI to help optimize queries, have it use `explain_query` first to get the actual execution plan, then iterate on the query based on real performance data rather than assumptions.
Quick info
- Author
- JaviMaligno
- License
- Runtime
- Python
- Transport
- stdio
- Category
- database
- Difficulty
- Intermediate
- Self-hostable
- β
- API key
- No API key needed
- Docker
- β
- Version
- 0.0.0
- Updated
- Jan 28, 2026
Client compatibility
- β Claude Code
- β Cursor
- β VS Code Copilot
- β Gemini CLI
- β Windsurf
- β Cline
- β JetBrains AI
- β Warp
Platforms
π macOS π§ Linux πͺ Windows