Postgres MCP Server

Ask Claude or Cursor about your Postgres data and slow queries, with the database protected: read-only transactions, timeouts, row caps and a statement checker.

Postgres MCP Server product illustration

What it does

Let Claude Code, Claude Desktop, Cursor or any MCP client explore and query a PostgreSQL database, and review its performance, without being able to change it.

  • Read-only by design. Every query runs inside BEGIN READ ONLY with statement_timeout, lock_timeout and a row cap, then rolls back. A statement checker refuses writes, multiple statements, COPY, DO, SET, data-modifying CTEs and functions that read server files or signal other sessions, before anything reaches the server.
  • Knows your schema. Tables with estimated rows and sizes, columns with defaults and comments, primary/foreign keys in both directions, check constraints, indexes with their scan counts, view SQL, sample rows.
  • Finds slow spots. explain_query turns a plan into findings (full scans, disk sorts, bad estimates). db_health lists unused indexes, foreign keys without an index, tables without a primary key, dead rows, never-analyzed tables, long-running queries. top_queries reads pg_stat_statements.
  • Works with PostgreSQL 13+ over a standard connection string (tested on PostgreSQL 14 and 18). Hosted Postgres (Supabase, Neon, RDS, Cloud SQL ...) connects the same way.

What you get

The screenshot is real output from the kit’s own commands. Tested from a fresh download of this exact zip: ./run_tests.sh ran 24 tests OK on Python 3.13 and 3.10.

Real terminal output from Postgres MCP Server: demo and passing tests

Tools

ToolWhat it returns
list_databasesconfigured connections (password hidden), server version, superuser warning
list_schemasschemas with table counts and sizes
list_tablestables, views, materialized views, foreign tables: estimated rows, size, comment
describe_tablecolumns, keys both ways, constraints, indexes + scans, view SQL, sample rows
read_queryone read statement with $1, $2 params; capped rows, truncated flag, timing
explain_queryplan tree + plain-English findings; analyze: true adds real timings (runs the query, read-only)
search_schematables and columns whose name contains a word
db_health9 checks from pg_stat_* with a short summary
top_queriesslowest / most frequent queries from pg_stat_statements (or how to enable it)
export_csvup to 100,000 rows of a read query into a new .csv (never overwrites)
write_queryonly with --allow-writes: one INSERT/UPDATE/DELETE/MERGE/DDL, committed

Install

Python 3.9+ and psycopg 3:

python3 -m pip install "psycopg[binary]"      # or: python3 -m pip install -r requirements.txt

Create a read-only user (recommended; see examples/read_only_role.sql):

CREATE ROLE mcp_reader LOGIN PASSWORD 'change-me';
ALTER ROLE mcp_reader SET default_transaction_read_only = on;
GRANT pg_read_all_data TO mcp_reader;    -- PostgreSQL 14+
GRANT pg_read_all_stats TO mcp_reader;   -- optional: other sessions in db_health / top_queries

If your host doesn't let you grant pg_read_all_data, use the per-schema GRANT SELECT lines in the same file. Claude Code

claude mcp add postgres -- python3 /path/to/postgres-mcp-server/server.py --dsn postgresql://mcp_reader:PASSWORD@localhost:5432/mydb

Or install the folder as a plugin (.claude-plugin/plugin.json, .mcp.json, skill postgres-analyst) and set PGMCP_DSN in your environment.

Claude Desktop / Cursor: copy examples/configs/claude_desktop_config.json or examples/configs/cursor-mcp.json and fill in the path and connection string. Several databases: repeat --dsn name=postgresql://... (or separate them with spaces in PGMCP_DSN). With no DSN the standard PGHOST, PGDATABASE, PGUSER, PGPASSWORD variables and ~/.pgpass are used.

Options: --max-rows 200 (cap per query), --timeout 10 (seconds, per statement), --allow-writes (adds write_query; the skill asks before every write).

Try it

./run_tests.sh                                             # unit tests, no database needed
PGMCP_TEST_DSN=postgresql://postgres:pw@localhost/scratch ./run_tests.sh   # + integration tests on a real server
PGMCP_DEMO_DSN=postgresql://postgres:pw@localhost/scratch ./demo.sh        # MCP session on a fictional shop

The integration tests and the demo create schema demo_shop in the database you point them at. Use a scratch database.

Honest limits

  • The model writes the SQL. Check the query it shows you before acting on a number.
  • Read-only means *this server* won't change data. Use a read-only role as well: a superuser can bypass a lot, and the server warns when it is connected as one.
  • explain_query with analyze: true really runs the query (inside a read-only transaction, with the timeout).
  • db_health counts are since the last statistics reset or restart, on the server you're connected to; check replicas separately before dropping an "unused" index.
  • top_queries needs the pg_stat_statements extension.
  • Not a migration tool, an ORM or a backup tool.

Format and license

Zip, 34 KB, v1.0, 25 files. You may use and modify this kit for your own work and for clients you serve, including paid client work. Not affiliated with or endorsed by the PostgreSQL Global Development Group, Anthropic or Cursor (Anysphere).

FAQ

Can the model change my data?
Not by default: every query runs in a READ ONLY transaction with a timeout, and writes, COPY, DO, SET and file/signal functions are refused before they reach the server. Writes need --allow-writes, and the skill asks you first.
Does it work with Supabase, Neon or RDS?
Yes, through a normal connection string; tested on PostgreSQL 14 and 18.
What do I need?
Python 3.9+ and psycopg 3 (pip install "psycopg[binary]").
How do I get the files?
Pay by card right here on youraitools.co: your download links appear on this page the moment the payment goes through (claim window: 72 hours, up to 5 downloads per 7 days). Need the files re-sent later? Email hello@youraitools.co.
Refunds?
Email hello@youraitools.co within 14 days if it doesn’t work and we can’t fix it.
Can I pay with crypto?
Yes. Pay the same price in USDC or USDT on Base, Ethereum, Arbitrum, Optimism, Polygon or Solana from the "Pay with crypto" button, with no account or KYC. We check the payment on-chain and show your download links as soon as it confirms.

Get Postgres MCP Server

PostgreSQL MCP server for Claude Code, Claude Desktop and Cursor: read-only transactions, schema, EXPLAIN findings, health checks, slow queries. Python. $29.

Save with a bundle

Pairs well with

Related

1 email a week: new x402 + MCP tools and free kits.

Short weekly digest of new x402 + MCP tools and free kits, including the MCP OAuth Checker. No spam, unsubscribe any time.