Postgres MCP Safe logo

Postgres MCP Safe

Read-only Postgres MCP access with transaction-level enforcement, schema inspection, query plans, and bounded results.

Install Postgres MCP Safe

Run the project’s own setup commands, in order.

Setup steps from the project

postgres-mcp-safe installs with its own steps rather than a config we can generate, so these are copied as-is. Follow them in order and run the commands in a terminal. Source: github.com

  1. 1. Start the server
    docker run -i --rm ghcr.io/poudel0/postgres-mcp-safe postgresql://host.docker.internal:5432/mydb
    

Overview

postgres-mcp-safe provides read-only access to PostgreSQL through MCP tools for queries, schema inspection, and query plans. It opens a fresh connection for every call, uses a read-only transaction, sends one parsed statement, and applies time, row, and response-size limits. PostgreSQL enforces the read-only boundary rather than a SQL parser, while superuser connections are refused unless explicitly allowed. Reach for it when an AI client needs database visibility without write access and you can provide a suitably restricted Postgres role.

Use cases

  • Query application data without permitting writes
  • Inspect tables, views, columns, constraints, and indexes
  • Generate PostgreSQL query plans for troubleshooting
  • Replace the deprecated server-postgres package in an MCP configuration

Key features

  • PostgreSQL-enforced read-only transactions
  • Fresh connection for every tool call
  • Schema and index inspection
  • Optional EXPLAIN ANALYZE
  • Timeout, row, and byte limits
  • Superuser refusal by default

Tools4

What Postgres MCP Safe lets your agent do. Expand a tool for its parameters.

Self-reported~54 tokens of context

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.

Adoption & maintenance

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

GitHub stars1Stargazers on the source repository.
npm downloads1.6kPackage downloads in the last 30 days.
Last commit18d agoMost recent push to the default branch.
Tools exposed4Callable tools this server registers over MCP.

How Postgres MCP Safe works

What postgres-mcp-safe does

postgres-mcp-safe exposes a PostgreSQL database to MCP-compatible AI clients while restricting database activity to read-only operations. It is designed as a replacement for the deprecated @modelcontextprotocol/server-postgres package, with a configuration that keeps the same basic command-and-URL pattern.

The server provides four tools. query accepts one SQL statement and returns rows as a JSON array. list_tables reports tables and views, including their schemas and types. describe_table returns columns, constraints, and indexes for a selected table. explain returns a query plan; running EXPLAIN ANALYZE requires an explicit opt-in because it executes the query.

How it works

Each tool call creates a new database connection and starts a READ ONLY transaction. SQL is sent as one protocol-level parse operation, so multiple statements such as COMMIT; DROP TABLE ... are rejected before execution. Statements that do not return rows, including SET, DO, COPY, CALL, and PREPARE, are rejected.

The server combines PostgreSQL transaction enforcement with a statement timeout, a client-side deadline, and response limits. By default, a call can return up to 1,000 rows and 262,144 JSON bytes. A single row is subject to the same byte ceiling. Connections close after each call, ending the transaction and releasing session-level effects such as advisory locks.

Transaction-level read-only protection is not a substitute for a restricted database role. Functions such as dblink_exec can open another session, and superusers may have capabilities outside ordinary table writes. The server therefore rejects superuser roles and members of superuser roles unless --allow-superuser is supplied.

Setup and configuration

Run the package with a PostgreSQL connection URL after the command. For example:

config.json
{
  "mcpServers": {
    "postgres": {
      "command": "npx",
      "args": ["-y", "postgres-mcp-safe", "postgresql://localhost/mydb"]
    }
  }
}

Flags must appear before the URL. Available controls include --timeout for the per-call time limit, --max-rows for the row limit, and --max-bytes for JSON and individual-row size limits. --allow-explain-analyze enables query execution during explain, while --allow-superuser permits a superuser connection and removes the guarantee that the connection is read-only. Limits cannot be disabled by setting them to zero.

The connection can use either a PostgreSQL URL or a keyword DSN. Standard libpq PG* environment variables, including PGPASSWORD, are also read. Docker users can run the published container with a connection URL, and binaries are available for Linux, macOS, and Windows.

A role such as mcp_ro can be granted pg_read_all_data on PostgreSQL 14 or later, or specific SELECT privileges on the required objects. Setting that role's default_transaction_read_only option adds database-side protection alongside the server's per-call transaction setting.

Tools and capabilities

  • query: Executes one read-only, row-returning SQL statement.
  • list_tables: Lists tables and views, optionally limited to a schema.
  • describe_table: Shows a table's columns, constraints, and indexes; the default schema is public.
  • explain: Produces a query plan, with analysis available only when enabled.
  • Result serialization preserves clear JSON types such as booleans, numbers, and JSON values; other PostgreSQL values are returned as strings.
  • Tool failures are returned as tool results with isError: true, along with the PostgreSQL message and SQLSTATE.

Limitations and notes

Every call pays the cost of opening a new connection. For a remote database over TLS, the documentation estimates roughly 0.5 seconds per call at 50 ms network latency. PostgreSQL 17 or newer can reduce one round trip when sslnegotiation=direct is added to the connection URL.

The server intentionally rejects non-row-returning statements and caps response size. EXPLAIN ANALYZE is potentially mutating through query execution, so it is disabled unless explicitly enabled. Allowing a superuser weakens the safety model; a dedicated read-only role is recommended when access to a database with sensitive server capabilities is involved.

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

postgres-mcp-safe is an MCP server that connects AI clients to PostgreSQL databases with read-only access. Its main tools query one SQL statement, list tables and views, describe columns and indexes, and explain query plans. Each call uses a fresh read-only transaction with limits on time, rows, and response size.

Run it with `npx -y postgres-mcp-safe` followed by a PostgreSQL connection URL. Put the command and arguments in the MCP client's server configuration, with any flags before the URL. Docker users can run the published container, and binaries are available for Linux, macOS, and Windows.

It starts every call in a PostgreSQL `READ ONLY` transaction and sends SQL as a single parsed statement. PostgreSQL rejects multiple statements and write operations, while statements that return no rows are refused. A dedicated read-only database role provides additional protection.

Only with the `--allow-superuser` flag. By default, connections using a superuser role or a role that is a member of one are refused because transaction-level read-only mode cannot cover every superuser capability. A restricted role such as one granted `pg_read_all_data` is recommended.

Yes. Cursor is named as a supported AI client, and the server uses the standard command-and-arguments MCP configuration pattern. Configure Cursor to launch `npx -y postgres-mcp-safe` with the PostgreSQL URL after any server flags.

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