blog · problem ·
Your Postgres MCP server's read-only mode can be bypassed with COMMIT
The old official Postgres MCP server wraps queries in a read-only transaction, but one COMMIT ends it and the rest of the SQL runs with write access. Here's why, and how to close it.
If you connect Claude, Cursor or another AI tool to Postgres, there's a good chance you use @modelcontextprotocol/server-postgres. It was the official server, it calls itself read-only, and it was still downloaded 143,342 times in the week of 9 to 15 November 2026.
It's deprecated, and its read-only mode can be broken with one word.
The bug
The server runs every query roughly like this:
BEGIN TRANSACTION READ ONLY;
-- your SQL goes here
ROLLBACK;
Your SQL is sent with node-postgres's client.query(sql), which uses Postgres's simple query protocol. One simple query message can hold many statements separated by ;. So the model, or someone steering it with a prompt injection, can send:
COMMIT; DROP TABLE customers;
COMMIT ends the read-only transaction. The Postgres docs spell out what happens next: when a simple query message contains a COMMIT that closes the current transaction, the statements after it run in a new implicit transaction. That new transaction isn't read-only, so DROP TABLE runs.
There's a quieter version too. COMMIT; SET statement_timeout TO 1; changes the session itself. The server keeps a connection pool, so the next caller that gets that connection inherits a 1 ms timeout.
Datadog Security Labs wrote this up on 21 August 2025. A fix was merged upstream in May 2025, but it was never published to npm. The last release, 0.6.2, is from December 2024. The package is now marked "Archived and unmaintained; no security guarantees", and there's no official replacement.
Why "just check the SQL" doesn't work
The tempting fix is to scan the SQL for COMMIT or DROP. Don't. SQL has comments, quoting, dollar-quoted strings, DO blocks and functions with side effects. A parser in your server will disagree with Postgres's parser somewhere, and that disagreement is where the next bypass lives.
The fix that holds is to make Postgres enforce the rules itself.
How I fixed it in postgres-mcp-safe
I built postgres-mcp-safe, a drop-in replacement written in Go. You change the package name in your MCP config and keep everything else. It never reads the SQL text to decide what's allowed. Instead it stacks checks that Postgres enforces:
- One statement only. Your SQL is sent only as a Parse message, the extended protocol that prepared statements use. Postgres refuses a Parse message with more than one statement, so
COMMIT; DROP ...fails with a syntax error before anything runs. - Only statements that return rows. After Parse, the server asks Postgres to Describe the statement. If it returns no columns, it's rejected. That blocks
COMMIT,SET,DO,CALL,PREPAREandCOPY ... TO PROGRAMwithout reading the text. - A real read-only transaction. Every call runs inside
BEGIN READ ONLY, so Postgres blocks writes, DDL andnextval(). ADELETE ... RETURNINGdoes return rows, so it passes step 2, and fails here instead. - A fresh connection per call. There's no pool, so nothing is left behind for the next caller. A leaked
SETor advisory lock dies with its connection. - Limits. A statement timeout set inside the transaction, plus caps on rows and bytes returned.
- No superusers by default. A superuser can get around a read-only transaction. For example,
lo_exportcan write a file on the database server. So the server refuses superuser roles, and members of them, unless you pass--allow-superuser.
The test suite runs attacks like these against a real Postgres, logged in as a superuser with --allow-superuser on (the worst case). After each one it checks that a canary table is still there:
| Attack | Result |
|---|---|
COMMIT; DROP SCHEMA public CASCADE; |
rejected: multiple commands (42601) |
COMMIT; SET statement_timeout TO 1; |
rejected: multiple commands; the next call isn't affected |
WITH d AS (DELETE FROM canary RETURNING *) SELECT * FROM d |
rejected: read-only transaction (25006) |
SELECT set_config('transaction_read_only','off',true) |
rejected (25001) |
DO $$BEGIN DELETE FROM canary; END$$ |
rejected: returns no rows |
COPY (SELECT 1) TO PROGRAM 'touch /tmp/pwn' |
rejected: returns no rows, and no file is created |
SELECT pg_sleep(600) |
cancelled by the timeout (57014) |
What it doesn't cover
Read-only here means read-only at the transaction level. A few things need the database role to stop them. dblink_exec opens its own session outside the transaction, and a superuser can read server files with pg_read_file. Connect with a read-only role, and the role blocks what the transaction can't. The next post shows how to set one up.
There's also a cost. A fresh connection per call means a new TLS handshake every time, about 0.5 s per call at 50 ms of network latency. On Postgres 17 and later you can save one round trip by adding sslnegotiation=direct to the URL.
If you use the old server today
Change one line in your MCP config:
- "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://localhost/mydb"]
+ "args": ["-y", "postgres-mcp-safe", "postgresql://localhost/mydb"]
Then connect with a read-only role instead of your admin login: How to give Claude or Cursor safe, read-only access to Postgres.