postgres-mcp-hardened

A hardened, read-only Rust replacement for the deprecated @modelcontextprotocol/server-postgres: an MCP server that lets an AI agent query PostgreSQL with defence in depth.

A drop-in Model Context Protocol server that lets an AI agent query PostgreSQL — read-only, enforced at the database level, with real SQL validation, timeouts, cost limits, OAuth 2.1, and an audit trail. Speaks Streamable HTTP and stdio, and negotiates the MCP revision: 2026-07-28 (current, and the default since upstream released it on 2026-08-03), 2025-11-25, and 2025-06-18 — what most shipping clients still speak today. A client asks for what it knows; it is not negotiated down.
Quickstart (60 seconds)
Add it to your MCP client (Claude Desktop, Cursor, Claude Code) — there is no Node runtime at run time,
the npm package is a launcher that fetches the native binary for your platform and verifies its
checksum before running it:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "postgres-mcp-hardened", "--stdio"],
"env": { "DATABASE_URL": "postgres://readonly_user:PASSWORD@localhost:5432/mydb" }
}
}
}
The connection string goes in env, not in args, on purpose: arguments show up in ps output and
in shell history on a shared machine, and a database password does not belong there. Use a
least-privilege read-only role (--print-setup-sql writes the statements). Other ways in: Install.
To try the whole thing against a real database first, docker compose -f examples/docker-compose.yml up -d
brings up PostgreSQL with sample data and the server in front of it, connecting as a role that holds
SELECT and nothing else.
Try to break it — one command, no database
The read-only guard has an offline mode. Hand it a statement and it says what it decided: no
database, no configuration, nothing installed permanently.
npx postgres-mcp-hardened --validate "/* comment */ DROP TABLE users"
# REJECT: non-read-only statement: Drop
npx postgres-mcp-hardened --validate "SELECT 1; DROP TABLE users"
# REJECT: multiple statements are forbidden
npx postgres-mcp-hardened --validate "WITH d AS (DELETE FROM t RETURNING *) SELECT * FROM d"
# REJECT: non-read-only statement: non-read-only query (CTE / SELECT INTO / FOR UPDATE)
npx postgres-mcp-hardened --validate "SELECT * FROM orders WHERE id = 1"
# ALLOW
If something that writes comes back ALLOW, that is the most valuable thing anyone can send us.
It needs no working exploit and no write-up — one line of SQL and "this should not be allowed" is a
complete report. Anything that gets past the guard goes through SECURITY.md;
everything else is an ordinary issue, and the bar for opening one is this looks wrong to me, not
I am certain.
The fuzzer is deterministic and prints its seed, so whatever it finds reproduces on a machine that
has never seen yours — a million mutations take about a minute:
npx postgres-mcp-hardened --fuzz 1000000
# fuzz: 1000000 iterations, seed 1592594996, slowest validation 8 ms
# RESULT: 0 invariant violations
Every bypass found so far lives in the MUST_REJECT corpus in src/validate.rs and runs on every
commit, recorded with what it cost rather than tidied away. Yours would join them.
@modelcontextprotocol/server-postgres is deprecated on npm (last publish December 2024) and
still sees hundreds of thousands of monthly downloads:
581,980 downloads in the 30 days to 28 November 2026 (npm).
Credit where it is due: its approach is not naive — it
wraps each query in BEGIN TRANSACTION READ ONLY and always ROLLBACKs, which is a real defence
and one this server now adopts as well. Its entire defence is that one database-level read-only
transaction, and that alone does not stop every write.
This server stacks the layers instead:
- AST validation (sqlparser) in front of the database: multi-statement input,
DROP, writing CTEs
and SELECT ... INTO are rejected before they are sent.
- The read-only transaction and the rollback, as in the original, plus a denylist for the
functions that write despite them.
- Timeouts and cost limits:
statement_timeout, idle_in_transaction_session_timeout and an
EXPLAIN cost guard.
- A start-up gate: no network listener when the role it was given can write.
- Authentication and audit: OAuth 2.1, and a tamper-evident hash-chained audit log.
- Prompt-injection handling: row data is wrapped
trusted="false" with delimiter escaping.
The problem is that it is the only defence, and it is not complete:
-
A read-only transaction does not block every write, and a rollback does not undo everything it
lets through. Two separate facts, and the second is the one that matters.
gin_clean_pending_list() runs inside SET TRANSACTION READ ONLY and its work survives the
rollback: an index with 25 pending pages has 0 after the transaction is rolled back.
pg_backup_start() puts the session into backup state, survives DISCARD ALL, and with the
default fast => false waits for a spread checkpoint while forcing full_page_writes on, which
is a real cost on a busy server. pg_import_system_collations() also executes without raising
SQLSTATE 25006, but be careful how much weight you put on it: that one IS undone by a
rollback, so against a server that always rolls back it is a curiosity rather than a bypass.
Reproduce it, but read the two preconditions first, because without them you will see a zero or an
error and conclude we made this up. All three need superuser or ownership of the object. And the
import only restores collations that are missing, so something has to be removed first:
-- as superuser, and note these are three separate transactions: a statement that errors
-- inside a block aborts the whole block, so they cannot be run as one.
DELETE FROM pg_collation WHERE oid IN (SELECT oid FROM pg_collation ORDER BY oid DESC LIMIT 200);
BEGIN READ ONLY;
DELETE FROM pg_collation WHERE collname LIKE 'zu%'; -- ERROR: cannot execute DELETE ...
ROLLBACK;
BEGIN READ ONLY;
SELECT pg_import_system_collations('pg_catalog'); -- 200, no error
COMMIT; -- and now the rows are there
Both are writes, both are inside a read-only transaction, and one is refused while the other is
not. That asymmetry is why this server does not treat the transaction as its only defence. It is
also why the role matters more than any of this: every example above needs privileges a
least-privilege reader does not have, and this server refuses to start as a network listener when
the role it was given can write. What it cannot control is which connection string somebody pastes
into a client config, and the usual answer is whichever one they already had.
-
No statement timeout, no cost guard, no row limit — one query can run until the server gives up.
-
No authentication, no audit trail, no handling of prompt injection through returned row data.
-
One source file of 143 lines, unmaintained since December 2024, no test suite.
This server keeps the rollback, adds AST validation in front of it, and adds the operational layers
the original never had.
Status: pre-1.0
This is 0.1.x, and it is meant to be read that way. The first release was 0.1.0 on 9 August 2026.
What it has been through, all of it recorded in CHANGELOG.md:
- Six rounds of audit or outside review, each with its findings and the query that reproduced
them: an external review of the tool surface and an independent reviewer (0.1.7); an outside review
of a draft article, four reviewers, six bypasses (0.1.8, finished in 0.1.10); the maintainers' own
audit of the released 0.1.10 (0.1.11); the maintainers' second audit of the released 0.1.11, with
618 live probes over PostgreSQL 13 to 18, and an independent review before the tag (0.1.12). An
earlier independent review is written up in
docs/AUDIT_2026-07-26.md.
- A deterministic fuzzer that prints its seed (
--fuzz, above), and a corpus of bypasses
(MUST_REJECT) that runs on every commit.
- Two published security advisories:
GHSA-xjfm-h4wm-qmp6
(0.1.10) and
GHSA-cxv3-jw58-9gr9
(0.1.11, fixed in 0.1.12). Both were found by us, in our own releases.
What it has not been through: nobody outside this project has run it against their own data.
Wanted: deployments on real, read-only databases, and reports of whatever looks wrong — a
bypass through SECURITY.md, anything else as an
issue. Known limits are documented, not
hidden, in THREAT_MODEL.md.
Latest release