# postgres-mcp-safe [Health: Active]

**Category:** 🗄️ Databases  
**Repository:** https://github.com/Poudel0/postgres-mcp-safe  
**GitHub Stars:** 1  
**npm Downloads (last month):** 1637  
**Views:** 0  
**Installs:** 0  
**Upvotes:** 0  
**Directory Page:** https://allmcps.com/mcp/postgres-mcp-safe

## Description
Safe, read-only Postgres MCP server. Drop-in replacement for @modelcontextprotocol/server-postgres, enforced by Postgres and safe by default (refuses superuser roles).

## Tools
Capabilities this server exposes over MCP:

- **query** — One read-only statement. Returns a JSON array of rows.
- **list_tables** — Tables and views with schema and type.
- **describe_table** — Columns, constraints and indexes.
- **explain** — Query plan. `analyze` needs `--allow-explain-analyze`.

## Claude Desktop Quick Installation
No generated config for this server: it installs through its own setup steps.

The project's own setup steps (from https://github.com/Poudel0/postgres-mcp-safe), copied verbatim. Follow them in order; they are not an mcpServers config.

1. Start the server:

```sh
docker run -i --rm ghcr.io/poudel0/postgres-mcp-safe postgresql://host.docker.internal:5432/mydb
```

## Documentation

## 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:

```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.

_Full upstream README: https://allmcps.com/mcp/postgres-mcp-safe/readme_

