Skip to main content
MCP server CX Codex

PostgreSQL MCP Server — Claude Code MCP server: query your database

Gives an agent access to PostgreSQL: queries, schema introspection, EXPLAIN plans and health checks. Read-only unless you opt in.

Install into Claude Code · Platform CLI All methods and platforms ↓
claude mcp add postgres-mcp -- npx -y @yawlabs/postgres-mcp
Open on GitHub

What PostgreSQL MCP Server does

The server connects through DATABASE_URL and exposes a set of tools: pg_query for SQL, pg_list_schemas, pg_list_tables and pg_describe_table for schema work, pg_explain for plans, plus pg_top_queries, pg_seq_scan_tables, pg_unused_indexes, pg_table_bloat and pg_inspect_locks for diagnostics. User SQL goes out over the extended query protocol, so each request carries a single statement. Bind values are passed as a params array for $1, $2 and so on.

Writes stay off until you set ALLOW_WRITES=1. A separate tool, pg_readonly, never leaves read-only mode no matter what that variable says, which suits hosts that gate tools one by one. The recommended setup is a least-privileged Postgres role, since the database then enforces the boundary itself. Running pg_explain with analyze: true on a non-SELECT needs ALLOW_WRITES=1 and rolls back afterwards, though a sequence the statement advanced stays advanced.

Version 0.12 brought pg_index_advisor, which costs candidate indexes with HypoPG hypothetical indexes and creates nothing on disk. Audit logging of SQL is off by default and never records bound parameter values. Node 22 or newer is required. On 0.12.0, upgrade: in pg_index_advisor SQL passed through the statements argument could run outside the read-only transaction, and hosts often auto-allow that tool because it carries a read-only annotation.

What it helps with

  • read someone else's schema without hand-writing pg_catalog joins
  • see a query plan and find where it slows down
  • spot sequential scans and indexes nothing uses
  • work out which session is holding a lock
  • hand an agent database access without risking dropped tables

Example prompts for the agent

  • List the tables in the public schema and describe orders
  • Run EXPLAIN with analyze on SELECT * FROM orders WHERE user_id = $1
  • Which indexes would help this query?

How to install PostgreSQL MCP Server

Platform CLI

claude mcp add postgres-mcp -- npx -y @yawlabs/postgres-mcp

Step by step, with commands for each platform: How to add an MCP server to Claude Code, Codex, Cursor, VS Code, Gemini CLI and more

Questions about PostgreSQL MCP Server

How do I install PostgreSQL MCP Server in Claude Code?

Run claude mcp add postgres-mcp -- npx -y @yawlabs/postgres-mcp. Then set your connection string in DATABASE_URL.

What does PostgreSQL MCP Server do?

It gives an agent tools to query PostgreSQL, inspect schemas, read EXPLAIN plans and check performance. Writes only happen when ALLOW_WRITES=1 is set.

How is it different from @modelcontextprotocol/server-postgres?

That package is archived and unmaintained, and it still carries a documented stacked-query injection. Here user SQL travels over the extended query protocol, so extra statements get rejected.

Similar MCP servers

All MCP servers for Data and databases →

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor