How to Use PostgreSQL MCP with Claude Code
Connect Claude Code directly to any PostgreSQL database to run queries, inspect schemas, analyze query plans, and generate migrations with natural language.
What is the PostgreSQL MCP?
The PostgreSQL MCP creates a secure tunnel between Claude Code and any Postgres-compatible database. Unlike ORMs or GUI tools, it lets Claude issue raw SQL, inspect execution plans, and reason about your schema at the database level — ideal for performance tuning, schema design, and data debugging.
Installation
mcpizy install postgres
Provide your connection string (e.g., postgresql://user:password@host:5432/dbname) during setup. SSL connections are supported and recommended for remote databases.
Key Capabilities
- Execute arbitrary SQL — SELECT, INSERT, UPDATE, DELETE, and DDL statements.
- Inspect schemas — list tables, columns, indexes, constraints, and foreign keys.
- Analyze query plans — run EXPLAIN ANALYZE and let Claude interpret the output.
- Generate migrations — describe a schema change in plain English; Claude writes the SQL.
- Monitor slow queries — query
pg_stat_statementsand identify bottlenecks.
Example Usage
Identify the slowest queries in your production database:
SELECT query, calls, total_exec_time / calls AS avg_ms
FROM pg_stat_statements
ORDER BY avg_ms DESC
LIMIT 10;
Claude runs this via the MCP, then proposes missing indexes or query rewrites based on the results.
Tips and Best Practices
- Create a read-only role for exploratory queries and a write role only for approved migrations.
- Use
BEGIN; ... ROLLBACK;blocks when testing destructive statements before committing. - Pair with the Supabase MCP if your Postgres instance is hosted on Supabase for management API access.
- For analytics workloads on large tables, consider the ClickHouse MCP instead.