Skip to content
MCP server

Pg Readonly MCP

Read-only Postgres access for agents that parses SQL and refuses write-capable roles

Runs locallyStatus and history
Install
Pricing
Open source
Platforms
CLI, self-hosted
Runs
Locally
Version
0.1.1
Listing updated
1 day ago

About Pg Readonly MCP

Pg Readonly MCP is an open-source MCP server for databases. It runs locally on your machine and is started by your AI client.

This server was written in response to a published bypass in a reference Postgres server, where wrapping queries in a read-only transaction could be escaped with SELECT 1; COMMIT; DROP SCHEMA public CASCADE. Here the safety does not rest on transaction mode. Queries are parsed with sqlglot into a syntax tree and must be exactly one statement, have a read-shaped outer statement (SELECT, UNION, INTERSECT, EXCEPT or a CTE built from those), and contain no write anywhere in the tree, which catches data-modifying CTEs such as a DELETE inside a WITH. Unrecognised shapes are refused.

A second, independent layer checks the connection at startup. The server will not start if the role is a superuser, can create databases or roles, can bypass row-level security, or holds any grant beyond SELECT. The DSN comes from the PG_READONLY_MCP_DSN environment variable, which must point at a SELECT-only role.

The README lists what it does not cover: an over-privileged string that slips past the startup check, and costly but legal queries, which are bounded by row caps and a statement timeout. It runs over stdio from a PyPI package.

Install

Run in a terminal

claude mcp add pg-readonly-mcp --env PG_READONLY_MCP_DSN='<PG_READONLY_MCP_DSN>' -- uvx pg-readonly-mcp

Add to claude_desktop_config.json

{
  "mcpServers": {
    "pg-readonly-mcp": {
      "command": "uvx",
      "args": [
        "pg-readonly-mcp"
      ],
      "env": {
        "PG_READONLY_MCP_DSN": "<PG_READONLY_MCP_DSN>"
      }
    }
  }
}

Add to ~/.cursor/mcp.json

{
  "mcpServers": {
    "pg-readonly-mcp": {
      "command": "uvx",
      "args": [
        "pg-readonly-mcp"
      ],
      "env": {
        "PG_READONLY_MCP_DSN": "<PG_READONLY_MCP_DSN>"
      }
    }
  }
}

Add to .vscode/mcp.json

{
  "servers": {
    "pg-readonly-mcp": {
      "type": "stdio",
      "command": "uvx",
      "args": [
        "pg-readonly-mcp"
      ],
      "env": {
        "PG_READONLY_MCP_DSN": "<PG_READONLY_MCP_DSN>"
      }
    }
  }
}

Add to ~/.codeium/windsurf/mcp_config.json

{
  "mcpServers": {
    "pg-readonly-mcp": {
      "command": "uvx",
      "args": [
        "pg-readonly-mcp"
      ],
      "env": {
        "PG_READONLY_MCP_DSN": "<PG_READONLY_MCP_DSN>"
      }
    }
  }
}

Add to your client's MCP settings (most use this shape)

{
  "mcpServers": {
    "pg-readonly-mcp": {
      "command": "uvx",
      "args": [
        "pg-readonly-mcp"
      ],
      "env": {
        "PG_READONLY_MCP_DSN": "<PG_READONLY_MCP_DSN>"
      }
    }
  }
}

Configuration

  • PG_READONLY_MCP_DSNPostgres connection string for a role that holds only SELECT. The server refuses to start otherwise.Required

What it does

SQL parsed, not scanned
Builds a syntax tree instead of pattern-matching text.
Single statement only
Multi-statement payloads are refused before reaching the database.
Deep write detection
Walks the whole tree to find writes hidden inside CTEs.
Startup privilege check
Refuses roles with superuser, create or non-SELECT grants.
Row caps and timeout
Limits the cost of legal but heavy queries.
Honest limits
Documents what the guards cannot catch.

Good for

  • Let an agent explore a production schema safely
  • Answer data questions with SELECT-only access
  • Replace a read-only transaction approach with a parser-based guard
  • Audit how a read-only MCP server is built

Pricing

Open source

Open-source Python package on PyPI

Prices change. Check les-k.github.io before you buy.

Pg Readonly MCP FAQ

What does it need to start?

A PG_READONLY_MCP_DSN connection string for a role that holds only SELECT.

Why not just use a read-only transaction?

The README shows a COMMIT inside a multi-statement query can end that transaction early.

Can it catch writes inside a CTE?

Yes, it walks every node of the parsed tree.

Is the privilege check complete?

No, the README says ownership, PUBLIC grants and RLS policies are not fully enumerated.

Is Pg Readonly MCP free?

Yes. Pg Readonly MCP is open source, so you can use and self-host it for free.

How do I add Pg Readonly MCP to Claude Code?

Run this command in your terminal: claude mcp add pg-readonly-mcp --env PG_READONLY_MCP_DSN='<PG_READONLY_MCP_DSN>' -- uvx pg-readonly-mcp

What are the best alternatives to Pg Readonly MCP?

The closest alternatives to Pg Readonly MCP are REA, Agent Roast, Midplane, Leash and Tinplate Edge AI Safety, Security & EU Compliance.

Alternatives to Pg Readonly MCP

  1. REA

    MCP server

    CLI and MCP server that lets coding agents reverse engineer apps and binaries

    Runs locally
    • Open source
    • 464K GitHub stars
    • Developer tools
  2. Agent Roast

    MCP server

    Offline review that flags agent changes which weaken tests, assertions or CI

    Runs locally
    • Open source
    • 4 Hacker News points
    • Developer tools
  3. Midplane

    MCP server

    Postgres gateway for AI agents with table grants, column masking and approval for writes

    Runs locally
    • Open source
    • 5 GitHub stars
    • Databases
  4. Leash

    MCP server

    Review what your AI touched, approve risky actions and undo supported changes

    • 1 tool
    • Remote
    • Free
    • 1 GitHub star
    • Security
  5. SecureCloud

    MCP server

    SentinelOne and IT security catalog, prices, quotes and checkout links for Israel

    • 7 tools
    • Remote
    • Free
    • Security