Pg Readonly MCP logo

Pg Readonly MCP

Read-only PostgreSQL access over MCP with SQL parsing, privilege checks, row limits, and statement timeouts.

Install Pg Readonly MCP

Pick your client, then add it in one click or paste the config.

STDIOPython
~/Library/Application Support/Claude/claude_desktop_config.json
claude_desktop_config.json
{
  "mcpServers": {
    "pg-readonly-mcp": {
      "command": "uvx",
      "args": [
        "pg-readonly-mcp"
      ],
      "env": {
        "PG_READONLY_MCP_DSN": "YOUR_VALUE_HERE"
      }
    }
  }
}

Settings → Developer → Edit Config, merge this into mcpServers, then fully quit and reopen Claude Desktop.

Values are written into the snippet above so you can paste it as is. They never leave your browser.

Claude Code command
claude mcp add --env PG_READONLY_MCP_DSN=YOUR_VALUE_HERE --transport stdio pg-readonly-mcp -- uvx pg-readonly-mcp

Run in your terminal. Add --scope project to share it with your team through .mcp.json.

We couldn’t confirm it starts correctly

We ran the install command but it didn't respond within our test window — this can mean a slow first-time install rather than a real problem.

uvx pg-readonly-mcp

No response to initialize.

This is an experimental automated check and can have false negatives — missing environment variables, a slow cold install, etc. It doesn’t necessarily mean something’s wrong. Last checked .

Overview

The pg-readonly-mcp MCP server exposes a single query tool for PostgreSQL SELECT-shaped statements. It parses each SQL request, requires exactly one statement, rejects write operations anywhere in the syntax tree, and verifies that the database role has only read privileges. Use it when an agent needs database visibility while writes must be blocked by both query validation and connection permissions.

Use cases

  • Inspect PostgreSQL data with an AI agent
  • Run bounded read-only SQL queries
  • Prevent agent-issued writes at the database connection
  • Analyze schemas through an MCP client

Key features

  • Single read-only query tool
  • SQL AST validation with sqlglot
  • Nested write detection
  • PostgreSQL role privilege checks
  • Configurable row cap and statement timeout

Security & health

What it can reach and how reliably it runs — from our own checks, not the maintainer’s claims.

ActiveLast checked

Recent health check succeeded.

13/13 checks healthy over the last 52d

Supply-chain signal

No high-severity advisories surfaced by our automated scan.

Critical 0High 0Medium 0Low 0

Scanned 1d ago via OSV.dev · pg-readonly-mcp (PyPI)

Adoption & maintenance

Factual signals from GitHub, npm, and our automated checks — not a rating.

Last commit2mo agoMost recent push to the default branch.
Install checkInconclusiveDidn't respond in our test window — often a slow first install.

How Pg Readonly MCP works

What pg-readonly-mcp does

The pg-readonly-mcp MCP server gives an AI client one database operation: query. It accepts a SQL statement for PostgreSQL, validates the statement before execution, and returns up to a configured number of rows. The intended use is inspection and analysis of database contents without allowing the agent to modify the database.

SQL safety is based on parsing rather than searching for suspicious text. The validator requires one statement and permits only read-oriented outer forms: SELECT, set operations such as UNION, INTERSECT, and EXCEPT, or a CTE based on those forms. It also walks the complete syntax tree to find data-modifying operations hidden inside nested expressions or CTEs.

How it works

The pg-readonly-mcp MCP server applies two separate controls. First, sqlglot parses the incoming SQL and rejects multiple statements, unsupported shapes, and write operations anywhere in the parsed tree. A statement that is unfamiliar to the validator is rejected instead of being treated as safe.

Second, startup checks inspect the PostgreSQL connection role. The server refuses to start when the role is a superuser, can create databases or roles, can bypass row-level security, or has grants beyond SELECT on tables it can see. These checks address common privilege mistakes but are not a complete PostgreSQL privilege audit.

The query tool also applies a maximum row count and a statement timeout. Their defaults are 1,000 rows and 5,000 milliseconds respectively.

Setup and configuration

Install the package with:

Terminal
pip install pg-readonly-mcp

A source checkout can be installed in editable mode with pip install -e .. Supply the PostgreSQL connection string through the --dsn option or the PG_READONLY_MCP_DSN environment variable. Using the environment variable keeps the password out of command-line arguments and client configuration files.

A typical stdio client configuration is:

config.json
{
  "mcpServers": {
    "pg-readonly-mcp": {
      "command": "pg-readonly-mcp",
      "env": {
        "PG_READONLY_MCP_DSN": "postgresql://readonly_role:...@host:5432/db"
      }
    }
  }
}

The configured role should have no privileges beyond SELECT; the server checks this condition during startup.

Tools and capabilities

The pg-readonly-mcp MCP server provides one tool:

  • query: Executes one validated, SELECT-shaped PostgreSQL statement, subject to the row cap and statement timeout.

The project includes unit tests for SQL validation that do not require a database, plus integration tests using a live PostgreSQL instance to check role attributes and grants.

Limitations and notes

Read-only enforcement does not make expensive queries free. A wide result within the row limit or a costly query plan can still consume database resources, although the row cap and timeout limit exposure.

The server controls database queries, not what an AI agent does with returned data. Any data readable by the configured role can be exposed to the agent. The startup privilege check also does not enumerate every possible PostgreSQL privilege path, including ownership, PUBLIC grants, or all row-level security behavior.

Finally, SQL parsing uses sqlglot, while PostgreSQL uses its own parser. A disagreement on an edge case is possible. The database role check provides an independent barrier if the parser and PostgreSQL interpret a statement differently.

Written by AllMCPs with AI assistance from the project’s README and listing data. How we build listings

Reviews

No reviews yet — be the first to share how it worked for you.

Frequently Asked Questions about Pg Readonly MCP

pg-readonly-mcp is an MCP server that connects AI clients to PostgreSQL using a single `query` tool. It accepts one SELECT-shaped SQL statement, parses the full syntax tree to reject writes and multiple statements, checks that the database role is read-only, and limits rows and execution time.

Install it with `pip install pg-readonly-mcp`. From a source checkout, use `pip install -e .`. The server requires a PostgreSQL connection string supplied through `--dsn` or the `PG_READONLY_MCP_DSN` environment variable.

Set `PG_READONLY_MCP_DSN` to the PostgreSQL connection string in the MCP client's server configuration, or pass the connection string with `--dsn`. The connected role must not have privileges beyond `SELECT`, or startup validation will refuse the connection.

No. The server permits only supported read-shaped statements and walks the entire parsed SQL tree for write operations. It rejects statements such as `DELETE` or `DROP`, including writes nested inside a common table expression.

Related MCP servers

Add a badge to your READMEShow it’s listed — live SVG, Markdown or HTML.

AllMCPs Directory Badge

Full Badge Customizer

Showcase your server listing on GitHub or your project documentation. Embed this dynamic SVG badge to highlight official listing status and live engagement.

Badge Style:
Live Dynamic SVG PreviewPg Readonly MCP AllMCPs Directory Badge
Markdown (GitHub README)
[![AllMCPs](https://allmcps.com/api/badge/pg-readonly-mcp?style=directory)](https://allmcps.com/mcp/pg-readonly-mcp)
HTML Embed
<a href="https://allmcps.com/mcp/pg-readonly-mcp"><img src="https://allmcps.com/api/badge/pg-readonly-mcp?style=directory" alt="Pg Readonly MCP on AllMCPs" /></a>