PostgreSQL MCP Server

Official

Read-only SQL access to Postgres — let your assistant inspect schemas and answer questions from real data.

Local serverstdioTypeScriptMIT 63.0k

What is the PostgreSQL MCP server?

The PostgreSQL MCP server does one thing with sensible restraint: it lets an AI assistant query a Postgres database read-only. Point it at a connection string and your assistant can list tables, inspect column types, and run SELECT statements — which turns "can you check how many users signed up last week?" from a Slack message to your data person into a ten-second answer.

The read-only design is the feature, not a limitation. Every query runs inside a transaction that gets rolled back, so even a hallucinated UPDATE can't do damage. That makes it one of the few database tools you can reasonably point at a production replica without sweating. (Point it at a replica, not the primary, all the same — a runaway analytical query can still hog resources.)

Where it shines is exploratory work. Schema archaeology on an inherited codebase — "which tables reference orders, and what's actually in order_metadata?" — becomes conversational. So does ad-hoc analytics: the assistant writes better SQL than most humans when it can first read the real schema instead of guessing at column names.

Like the other Anthropic reference servers, it now lives in the archived repository, so expect stability rather than new features. Community successors (like Crystal DBA's postgres-mcp, which adds index tuning and health checks) pick up where it stops. For straightforward read-only querying, the reference server remains the simplest thing that works — and simplicity is exactly what you want between a language model and your database.

Setup notes worth knowing

  • The connection string is passed as a command-line argument, so it lands in your process list and your client config file in plaintext. Create a dedicated role holding only CONNECT and SELECT grants instead of reusing an application user.
  • Each table is exposed as an MCP resource carrying its schema, which means the assistant can read column names and types without you pasting DDL into the conversation.
  • Rows come back as JSON, which gets expensive on wide tables. Nudge the model toward explicit column lists and a LIMIT while it's still exploring.
  • Catalog views are readable too, so questions like "which indexes are unused?" or "what's the largest table?" work against pg_stat_user_indexes and friends with no extra tooling.

What you can do with it

Conversational analytics

Ask questions in English; the assistant reads the schema and writes correct SQL against real column names.

Schema exploration

Understand an unfamiliar database — relationships, types, conventions — without opening a GUI client.

Debugging with data

Check what's actually stored for a user or order while diagnosing a bug, straight from your editor.

Available tools

ToolWhat it does
queryRun a read-only SQL query; results return as JSON

How to install the PostgreSQL MCP server

{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://user:password@localhost:5432/mydb"
      ]
    }
  }
}

The connection string is passed as an argument — use a read-only role and a replica where possible.

Configuration

Node.js 18+ and a reachable Postgres instance. Strongly recommended: a dedicated read-only database role.

Example prompts to try

  • What tables exist in this database, and how are orders and customers related?
  • How many signups did we get per week over the last three months?
  • Show me the ten most recent rows in the payments table where status is 'failed'.

Frequently asked questions

No. Queries execute in read-only transactions that are rolled back, so INSERT/UPDATE/DELETE statements can't take effect. For defence in depth, connect with a role that only has SELECT grants anyway.