Readonly Postgres MCP logo

Readonly Postgres MCP

Runs PostgreSQL queries through MCP with read-only enforcement, schema discovery, row limits, and optional named query catalogs.

Install Readonly Postgres MCP

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

STDIONode.js
~/Library/Application Support/Claude/claude_desktop_config.json
claude_desktop_config.json
{
  "mcpServers": {
    "readonly-postgres-mcp": {
      "command": "npx",
      "args": [
        "-y",
        "readonly-postgres-mcp"
      ],
      "env": {
        "DATABASE_URL": "YOUR_VALUE_HERE",
        "PG_URL": "YOUR_VALUE_HERE",
        "PG_HOST": "YOUR_VALUE_HERE",
        "PG_DATABASE": "YOUR_VALUE_HERE",
        "PG_USERNAME": "YOUR_VALUE_HERE",
        "PG_PASSWORD": "YOUR_VALUE_HERE",
        "PG_SSL_MODE": "YOUR_VALUE_HERE",
        "PG_QUERY_REGISTRY": "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 DATABASE_URL=YOUR_VALUE_HERE --env PG_URL=YOUR_VALUE_HERE --env PG_HOST=YOUR_VALUE_HERE --env PG_DATABASE=YOUR_VALUE_HERE --env PG_USERNAME=YOUR_VALUE_HERE --env PG_PASSWORD=YOUR_VALUE_HERE --env PG_SSL_MODE=YOUR_VALUE_HERE --env PG_QUERY_REGISTRY=YOUR_VALUE_HERE --transport stdio readonly-postgres-mcp -- npx -y readonly-postgres-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.

npx -y readonly-postgres-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

readonly-postgres-mcp exposes PostgreSQL data to MCP clients through read-only query, schema description, and optional named-query tools. It accepts a connection URL or discrete PostgreSQL settings and runs as a stdio server through npm or npx. SQL is restricted to SELECT, non-modifying WITH clauses, and EXPLAIN, with statement timeouts and database-enforced row caps. Use it when an agent needs database visibility without an application-level write API.

Use cases

  • Query analytics and reporting data from an MCP client
  • Inspect PostgreSQL tables, views, columns, and constraints
  • Expose approved report queries through a local catalog
  • Let agents explore database metadata without write access

Key features

  • Read-only PostgreSQL MCP tools
  • Ad-hoc parameterized SQL queries
  • Relation and column metadata discovery
  • Optional named query registry
  • Database-enforced row limits
  • Configurable TLS and statement timeouts

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.

7/7 checks healthy over the last 27d

Supply-chain signal

No high-severity advisories surfaced by our automated scan.

Critical 0High 0Medium 0Low 0

Scanned 9d ago via OSV.dev · readonly-postgres-mcp (npm)

Adoption & maintenance

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

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

How Readonly Postgres MCP works

What readonly-postgres-mcp does

readonly-postgres-mcp connects MCP clients to PostgreSQL and provides tools for inspecting relations and retrieving query results. Its primary interface includes pg_query_sql for ad-hoc read queries and pg_describe for listing relations or examining a table or view. An optional pg_query tool exposes pre-approved queries from a local catalog when PG_QUERY_REGISTRY is configured.

The package also exports a PgReadonlyClient for application code and a NestJS module integration. Those APIs support running registered queries, issuing parameterized SQL, and closing the database client.

How it works

The MCP process can be installed from npm or launched with npx. It reads PostgreSQL connection details from DATABASE_URL, PG_URL, or individual PG_* variables. PG_URL takes precedence over DATABASE_URL, while individual settings override matching URL components.

Read protection is implemented in layers. A SQL guard permits only SELECT, non-data-modifying WITH, and EXPLAIN; the connection uses read-only transactions; and the database should use a role granted SELECT without write privileges. MCP tools are marked with the MCP readOnlyHint annotation.

Each MCP query uses a 15-second tool timeout and a default result limit of 1,000 rows. The row limit is applied inside PostgreSQL by wrapping the query, allowing the response to report whether additional rows exist. The underlying settings are controlled by PG_STATEMENT_TIMEOUT_MS and PG_MAX_ROWS.

Setup and configuration

Install the package with npm install readonly-postgres-mcp, or run it directly with npx readonly-postgres-mcp. Configure either a PostgreSQL URL or the discrete host, port, database, username, and password variables. PG_SSL_MODE accepts libpq-style values such as require, verify-ca, and verify-full. For non-local hosts, the default enables TLS without certificate verification unless a mode is selected.

Use a dedicated database role with SELECT privileges. A Cursor configuration can launch the server with npx, pass the package name as an argument, and provide connection variables in the process environment.

To restrict access to approved queries, set PG_MCP_ALLOW_ADHOC=false to hide pg_query_sql. Set PG_QUERY_REGISTRY to a registry file to enable named catalog queries. Catalog SQL is checked by the same guard when the server starts.

Tools and capabilities

  • pg_query_sql runs one parameterized SELECT, WITH, or EXPLAIN statement when ad-hoc access is enabled.
  • pg_describe lists relations in the configured search path or returns columns, types, nullability, defaults, primary keys, comments, row estimates, and partition information for a relation.
  • pg_query runs a registered catalog query by queryId when a registry is configured.
  • EXPLAIN ANALYZE is disabled by default because it executes the explained statement; it can be enabled with PG_ALLOW_EXPLAIN_ANALYZE=true.

Limitations and notes

One statement is accepted per call, and a trailing second statement or semicolon is not needed. Write operations and data-modifying CTEs are rejected before reaching PostgreSQL, including inserts, updates, deletes, schema changes, grants, maintenance commands, and SELECT INTO.

The SQL guard is not a complete security boundary: some database functions, including dblink and nextval, can bypass assumptions made by a read-only transaction. The database role is therefore the primary security boundary. Review the project’s security guidance and verify that writes fail for the configured role before using the server with sensitive data.

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 Readonly Postgres MCP

readonly-postgres-mcp is an MCP server that connects AI clients to PostgreSQL using read-only access. Its main tools run approved SQL with pg_query_sql, inspect tables and columns with pg_describe, and optionally execute registered queries with pg_query. It also applies SQL checks, read-only transactions, timeouts, and row limits.

Install it with `npm install readonly-postgres-mcp` or run it without a global install using `npx readonly-postgres-mcp`. Configure PostgreSQL credentials with `DATABASE_URL`, `PG_URL`, or the individual `PG_*` variables before launching the MCP process.

Yes, readonly-postgres-mcp can be launched from Cursor using an MCP configuration with `npx` as the command and the package name as an argument. Put the PostgreSQL connection variables in the configuration’s `env` object.

Set `PG_MCP_ALLOW_ADHOC=false` to hide and disable the `pg_query_sql` tool. You can then expose approved queries by setting `PG_QUERY_REGISTRY` to a catalog registry file.

Yes, but only after setting `PG_ALLOW_EXPLAIN_ANALYZE=true`. Plain `EXPLAIN` is available by default, while `EXPLAIN ANALYZE` is rejected by default because it executes the statement it examines.

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 PreviewReadonly Postgres MCP AllMCPs Directory Badge
Markdown (GitHub README)
[![AllMCPs](https://allmcps.com/api/badge/readonly-postgres-mcp?style=directory)](https://allmcps.com/mcp/readonly-postgres-mcp)
HTML Embed
<a href="https://allmcps.com/mcp/readonly-postgres-mcp"><img src="https://allmcps.com/api/badge/readonly-postgres-mcp?style=directory" alt="Readonly Postgres MCP on AllMCPs" /></a>