Back to Browse

Pgleash MCP Server

Developer ToolsLow Risk10.0MCP RegistryLocal
Free

Server data from the Official MCP Registry

Postgres proxy for AI agents: read-only by default, blocks DROP and unbounded DELETE, caps rows.

About

Postgres proxy for AI agents: read-only by default, blocks DROP and unbounded DELETE, caps rows.

Security Report

10.0
Low Risk10.0Low Risk

Valid MCP server (2 strong, 3 medium validity signals). No known CVEs in dependencies. Package registry verified. Imported from the Official MCP Registry.

7 files analyzed · 1 issue found

Security scores are indicators to help you make informed decisions, not guarantees. Always review permissions before connecting any MCP server.

Permissions Required

This plugin requests these system permissions. Most are normal for its category.

file_system

Check that this permission is expected for this type of plugin.

What You'll Need

Set these up before or after installing:

Connection string of the real database, ideally a least-privilege roleRequired

Environment variable: PGLEASH_UPSTREAM

How to Install

Add this to your MCP configuration file:

{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": [
        "-y",
        "pgleash@0.1.1"
      ],
      "env": {
        "PGLEASH_UPSTREAM": "your-pgleash-upstream-here"
      }
    }
  }
}

Some servers take additional arguments. See the repository for details.

Documentation

View on GitHub

From the project's GitHub README.

pgleash

CI pgleash MCP server

A Postgres proxy that keeps AI agents on a leash.

Point your coding agent at pgleash instead of at your database. pgleash parses every statement with Postgres's own parser and stops the dangerous ones before they reach the server. Reads go through. DROP, TRUNCATE, a DELETE with no WHERE, writes hidden inside a CTE or an EXPLAIN ANALYZE, and the stacked COMMIT; trick do not.

psql connected through pgleash: a count runs, while DELETE, DROP and a denied column come back as Postgres errors

Or try the rules without a database:

$ npx pgleash check "SELECT 1; COMMIT; DROP TABLE users"
blocked [multi-statement]: send one statement at a time (this query has 3)

Why

Giving an agent database access usually means one of two things: the real connection string, or an MCP server with a "read-only" switch inside it. Both have failed in public.

  • In July 2025 an AI agent on Replit deleted a production database during a code freeze.
  • Anthropic's reference Postgres MCP server wrapped each query in BEGIN TRANSACTION READ ONLY. Datadog Security Labs showed that a query could end that transaction itself and then run any SQL. The server has been archived, and the npm package still had about 21,000 weekly downloads when they wrote it up.
  • AWS's Postgres MCP server needed a fix for an incomplete list of blocked SQL (CVE-2025-59333).

pgleash enforces the rules on the wire, outside the agent. The agent connects with a pgleash token and never sees the real credentials.

Quick start

export PGLEASH_UPSTREAM='postgres://readonly_user:secret@db.internal:5432/app?sslmode=require'
npx pgleash
pgleash 0.1.0 (read-only, max 1000 rows, 10s statement timeout)
upstream:  db.internal:5432/app as readonly_user (sslmode=require)
listening: 127.0.0.1:6433

Give your agent this connection string (not the upstream one):
  postgres://agent:Yq3v...@127.0.0.1:6433/app

pgleash speaks the Postgres wire protocol, so psql, node-postgres, psycopg and ORMs connect to it like any other database. (The test suite uses node-postgres.) When a query is blocked, the agent gets a normal Postgres error with the reason and a hint:

app=> DELETE FROM orders;
ERROR:  pgleash blocked this query: DELETE is not allowed in read-only mode
HINT:  pgleash only lets this connection run the statements its mode allows. Ask a person to run this one.

As an MCP server

For Claude Code:

claude mcp add pgleash -e PGLEASH_UPSTREAM='postgres://...' -- npx -y pgleash mcp

For Cursor and other MCP clients:

{
  "mcpServers": {
    "pgleash": {
      "command": "npx",
      "args": ["-y", "pgleash", "mcp", "--deny-column", "email"],
      "env": { "PGLEASH_UPSTREAM": "postgres://..." }
    }
  }
}

MCP mode gives the agent three tools: query, list_tables and describe_table. Every call goes through the same proxy and the same rules.

What it does

Read-only by default. Only reads, EXPLAIN, SHOW, cursors and transaction control get through. The session is also pinned with default_transaction_read_only = on, and the agent can't undo that: SET, RESET ALL, DISCARD ALL, BEGIN READ WRITE and set_config() are blocked. So a write the parser can't see, such as one inside a user-defined function, still fails in the database.

Guarded writes when you want them. --mode guarded-write allows INSERT, plus UPDATE and DELETE with a WHERE clause that names a column. WHERE true and WHERE 1 = 1 don't count. DDL, TRUNCATE, COPY, DO, CALL, GRANT and MERGE stay blocked.

Off-limits tables and columns. --deny-table api_tokens blocks every reference to the table. --deny-column email blocks queries that name the column, pass a whole row to a function (row_to_json(u)), or SELECT * from a table that has it. pgleash looks those tables up in the live schema and refreshes the list every minute. As a second layer, any result column with a denied name comes back as NULL.

Dangerous functions blocked. These include pg_terminate_backend, pg_read_file, lo_import, dblink, pg_sleep, advisory locks, query_to_xml (which runs SQL passed in as a string), replication slot functions and setval. Add your own with --deny-function, and end a name with * to block a whole prefix.

Limits. Each result stops at 1,000 rows by default, and the agent gets a notice saying how many rows the query really returned. There is a 10 second statement timeout and a 60 second idle-in-transaction timeout.

Audit log. --audit pgleash.jsonl writes one line per query, allowed or blocked, with the rule that blocked it.

{"ts":"2026-11-29T10:02:11.412Z","session":3,"user":"agent","app":"psql","kind":"query","sql":"DROP TABLE users","allowed":false,"rule":"statement-type","reason":"DROP is not allowed in read-only mode"}

Configuration

Flags, environment variables or a pgleash.json in the working directory:

{
  "upstream": "postgres://readonly_user:secret@db.internal:5432/app?sslmode=verify-full&sslrootcert=/etc/ssl/rds-global-bundle.pem",
  "mode": "read-only",
  "maxRows": 500,
  "statementTimeout": "5s",
  "denyTables": ["api_tokens", "billing.cards"],
  "denyColumns": ["email", "phone", "password_hash"],
  "denyFunctions": ["app_purge_*"],
  "audit": "pgleash.jsonl"
}
FlagDefault
--upstream <url>$PGLEASH_UPSTREAM, then $DATABASE_URLThe real database. sslmode and sslrootcert work as in libpq.
--host, --port127.0.0.1, 6433Where pgleash listens.
--token <token>$PGLEASH_TOKEN, or randomThe password the agent uses.
--moderead-onlyOr guarded-write.
--max-rows <n>10000 turns the cap off.
--statement-timeout <t>10s
--deny-table, --deny-column, --deny-functionRepeat for more than one.
--allow-setting <name>Lets the agent SET another setting.
--allow-multi-statementoffEach statement is still checked.
--audit <file>JSON lines.

pgleash check runs the same policy without a database, so you can try rules or use it in CI and agent hooks. It exits 1 when a query is blocked, and --json prints the decision.

How it works

agent ──(token)──▶ pgleash ──(real credentials, TLS)──▶ Postgres
                     │
                     ├─ parse with libpg-query (Postgres's own parser)
                     ├─ check statement types, functions, tables, columns
                     └─ cap rows, null denied columns, write the audit log

pgleash speaks the Postgres wire protocol on both sides and checks the simple and the extended (prepared statement) protocol. A blocked statement isn't simply dropped. pgleash swaps it for a query that fails on the server, then replaces that error with its own. So the error comes back in the order the client expects, and an open transaction is aborted just as Postgres would abort it. Anything the parser can't parse is blocked.

Limits

pgleash is a guardrail, not a sandbox. Use it together with these:

  • A least-privilege role for the upstream. For read-only work, something like:

    CREATE ROLE agent_ro LOGIN PASSWORD '...';
    GRANT CONNECT ON DATABASE app TO agent_ro;
    GRANT USAGE ON SCHEMA public TO agent_ro;
    GRANT SELECT ON orders, products TO agent_ro;
    GRANT SELECT (id, name, created_at) ON users TO agent_ro; -- leaves out email and phone
    

    Grant column lists rather than revoking columns: a column REVOKE has no effect while the role holds SELECT on the whole table.

  • Keep the real connection string away from the agent. If it's in a .env file the agent can read, the agent can skip pgleash. On AWS, let only the pgleash host reach the database through its security group.

pgleash also has gaps you should know about:

  • Tables and columns are matched by name. A view over a denied table, or a view that renames a denied column, isn't caught. Deny the view too, or use column privileges.
  • In guarded-write mode, a user-defined function can write whatever its owner can. Block such functions with --deny-function.
  • The agent's side of the connection is plain TCP, meant for loopback. pgleash won't listen on another address without a token.
  • Not supported yet: COPY, fast-path function calls, replication connections, and TLS between the agent and pgleash.

Development

npm install
npm test        # starts an embedded Postgres; or set PGLEASH_TEST_URL to a superuser URL
npm run build

The tests run a real Postgres with TLS, connect through pgleash with node-postgres, and check each guardrail end to end. They also cover the CLI and MCP mode.

Security

If a query gets past the rules, please open an issue. For anything that exposes credentials or another session, see SECURITY.md for private reporting.

License

MIT

Reviews

No reviews yet

Be the first to review this server!