# readonly-postgres-mcp [Health: Active]

**Category:** 🗄️ Databases  
**Repository:** https://github.com/amar141989-dev/readonly-postgres-mcp  
**GitHub Stars:** 0  
**npm Downloads (last month):** 62  
**Views:** 0  
**Installs:** 0  
**Upvotes:** 0  
**Directory Page:** https://allmcps.com/mcp/readonly-postgres-mcp

## Description
Let AI read your PostgreSQL database - without letting it write to it. A read-only PostgreSQL MCP server.

## Claude Desktop Quick Installation
Install path detected from listing signals. Uses `npx` (confidence: high):

```json
"mcpServers": {
  "readonly-postgres-mcp": {
    "command": "npx",
    "args": ["-y","readonly-postgres-mcp"],
    "env": {
      "DATABASE_URL": "",
      "PG_URL": "",
      "PG_HOST": "",
      "PG_DATABASE": "",
      "PG_USERNAME": "",
      "PG_PASSWORD": "",
      "PG_SSL_MODE": "",
      "PG_QUERY_REGISTRY": ""
    }
  }
}
```

**Requires environment variables:** `DATABASE_URL`, `PG_URL`, `PG_HOST`, `PG_DATABASE`, `PG_USERNAME`, `PG_PASSWORD`, `PG_SSL_MODE`, `PG_QUERY_REGISTRY` — the values above are empty placeholders; fill in real credentials before running (see the repository for what each one is for).

## Documentation

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

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

