Back to MCP Servers

PostgreSQL MCP Server

Read-only PostgreSQL access for LLMs. Execute SQL queries inside read-only transactions and inspect table schemas as MCP resources.

Developer Tools by Anthropic / Model Context Protocol None (connection string with optional embedded credentials) active
Overview

The PostgreSQL MCP server is one of the original reference implementations from the Model Context Protocol team. It gives an LLM read-only access to a PostgreSQL database: the model can execute SQL queries (wrapped in READ ONLY transactions) and discover table structures through schema resources. It is intentionally narrow in scope, designed to demonstrate a safe, well-bounded MCP server rather than a full-featured database admin tool.

The server exposes a single tool, query, which accepts an SQL string and runs it inside a read-only transaction so writes are rejected by the database engine itself. Alongside that, each table in the connected database is published as a resource at postgres://<host>/<table>/schema, returning JSON describing column names and data types. This makes it straightforward for an agent to list tables, learn their schemas, and then issue informed SELECT statements.

Note: this server has been moved to the modelcontextprotocol/servers-archived repository. It is still installable via npm and Docker, but is no longer actively developed by the MCP steering group. For active maintenance and write capabilities, see the alternatives section.

Tools

Tool Description
query Execute a read-only SQL query against the connected PostgreSQL database. Runs inside a READ ONLY transaction so any write statements are rejected by Postgres.
Setup Guide

Prerequisites

  • A running PostgreSQL database reachable from the machine running the MCP client
  • A connection string in the form postgresql://user:password@host:port/db-name
  • Node.js (for the NPX install method) or Docker

Claude Desktop (NPX)

Add to claude_desktop_config.json:

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

Claude Desktop (Docker)

{
  "mcpServers": {
    "postgres": {
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "mcp/postgres",
        "postgresql://host.docker.internal:5432/mydb"
      ]
    }
  }
}

On macOS, use host.docker.internal instead of localhost so the container can reach the host-side Postgres process.

VS Code

Add the same server entry to your User Settings (JSON) under mcp.servers, or place it in a .vscode/mcp.json file in your workspace.

Building the Docker image locally

docker build -t mcp/postgres -f src/postgres/Dockerfile .

The package is licensed under MIT.

Use Cases
  • Let an AI agent answer business questions by querying a Postgres analytics database without risk of writes
  • Auto-generate SQL from natural language, then execute against a staging database to validate results
  • Schema discovery and documentation: have the agent enumerate tables and produce a data dictionary from the exposed schema resources
  • Ad-hoc data exploration during product or support investigations, with hard guarantees that no rows can be modified
  • Power read-only data Q&A inside internal tools or chat assistants backed by an existing Postgres warehouse
Example Prompts
  • "List all tables in the connected database and summarize what each one stores."
  • "What were total orders by week for the last 8 weeks? Use the orders table."
  • "Find the top 10 customers by revenue in 2026 and include their email addresses."
  • "Describe the columns and types of the users table, then write a query that counts signups per month."
  • "Run an EXPLAIN on SELECT * FROM events WHERE created_at > now() - interval '7 days' and tell me if it uses an index."
Pros
  • Officially built by the MCP team as a reference implementation, so the protocol usage is canonical
  • True read-only safety: queries run inside a READ ONLY Postgres transaction, enforced by the database engine, not just by prompt
  • Minimal surface area and easy to audit: one tool, one resource pattern
  • Available as both an npm package and a prebuilt Docker image
Limitations
  • Now archived in modelcontextprotocol/servers-archived; no longer actively maintained by the MCP steering group
  • Read-only only: no inserts, updates, schema changes, or transaction control
  • Very thin feature set (single query tool, no parameterized queries, no row limits, no result pagination)
Alternatives