blog · solution ·
How to give Claude or Cursor safe, read-only access to Postgres
A read-only role, an MCP server that enforces read-only on its own, and limits on what comes back. The setup takes about five minutes.
Letting an AI tool query your database is useful. It can read your schema, write the query you were about to write, and explain why a slow one is slow. But the model runs whatever SQL it decides on, and a prompt injection hidden in your data (a support ticket, a product review) can decide for it. So read-only has to be enforced by something the model can't talk its way past.
This is the setup I use. It has two layers: a Postgres role that can't write, and an MCP server that enforces read-only by itself. Each layer has gaps on its own, and each covers the other's.
Step 1: Create a read-only role
Connect as an admin and run:
CREATE ROLE mcp_ro LOGIN PASSWORD 'choose-a-password';
GRANT pg_read_all_data TO mcp_ro; -- Postgres 14+; on older versions, GRANT SELECT on what it needs
ALTER ROLE mcp_ro SET default_transaction_read_only = on;
What each line does:
pg_read_all_datagives SELECT on every table and view, and nothing else. No INSERT, UPDATE, DELETE or schema changes.default_transaction_read_only = onmakes every transaction this role starts read-only, as a second lock.- The role isn't a superuser, and that matters. A superuser can read and write files on the database server, even inside a read-only transaction.
If the model only needs a few tables, skip pg_read_all_data and grant SELECT on just those. A smaller schema also means less for the model to read through.
Step 2: Use an MCP server that enforces read-only itself
The old official server, @modelcontextprotocol/server-postgres, is deprecated, and its read-only mode can be broken with COMMIT; DROP TABLE ... (here's why). I use postgres-mcp-safe, a drop-in replacement I built for this.
Add it to your MCP client's config. Claude Desktop and Cursor both use this mcpServers shape:
{
"mcpServers": {
"postgres": {
"command": "npx",
"args": ["-y", "postgres-mcp-safe", "postgresql://mcp_ro@localhost:5432/mydb"],
"env": { "PGPASSWORD": "choose-a-password" }
}
}
}
The password goes in PGPASSWORD, not the URL. The server reads the standard PG* environment variables the same way psql does, and a password in the URL would show up in your process list.
No Node.js? Use Docker instead:
docker run -i --rm -e PGPASSWORD ghcr.io/poudel0/postgres-mcp-safe postgresql://[email protected]:5432/mydb
-e PGPASSWORD passes the password from your environment into the container.
There are also plain binaries for Linux, macOS and Windows on the releases page.
Step 3: Set the limits
Flags go before the URL:
| Flag | Default | What it does |
|---|---|---|
--timeout |
30s |
Time limit per call |
--max-rows |
1000 |
Rows returned per call; extra rows are cut off with a note |
--max-bytes |
262144 |
JSON bytes returned per call, and the max size of one row |
--allow-explain-analyze |
off | Lets explain run EXPLAIN ANALYZE |
--allow-superuser |
off | Allows a superuser role. Read-only is then not guaranteed |
You can't turn the limits off, and 0 is an error. That's on purpose. A model that runs SELECT * FROM events on a huge table shouldn't hang your database or flood its own context.
EXPLAIN ANALYZE is off by default because it actually runs the query, not just plans it. Plain EXPLAIN always works. If you turn ANALYZE on, it still runs inside a read-only transaction.
Step 4: Check it works
Ask your AI tool to run these. Each one should fail:
COMMIT; DROP TABLE some_table;should fail withcannot insert multiple commands into a prepared statement.DELETE FROM some_table RETURNING *should fail withcannot execute DELETE in a read-only transaction.
If every call fails with a superuser error, you're still connected as an admin user, like Docker's default postgres. That's the server refusing an unsafe setup, not a bug. Switch to mcp_ro.
What the model can do
The server gives the model four tools: query for read-only SQL, list_tables, describe_table (columns, constraints and indexes), and explain. It also serves each table's schema as a resource, the same way the old server did, so existing setups keep working.
The trade-off
Every call opens a fresh connection, so nothing from one call can leak into the next. Locally that's about 14 ms per call. Over the network with TLS it's about 0.5 s per call at 50 ms latency. On Postgres 17 and later, add sslnegotiation=direct to the URL to save one round trip.
For a tool that runs a handful of queries per question, that's a cost worth paying for a database that can't be changed through a chat window.