Read-only PostgreSQL access for an AI agent, and what a read-only role still does
Three things are called read-only in PostgreSQL. One COMMIT undoes the first, one SET undoes the second, and the third still wrote a row in my test.
How this was tested
Every statement and error on this page was run on PostgreSQL 18.6 in Docker on 21 and 22 November 2026, through psql, as four roles: a superuser, an application role that can write, an assistant role that holds pg_read_all_data, and an analyst role with explicit grants. The database has 1,000 customers and 100,000 orders. Where the page states a fact about another version, it says so.
Three different things are called read-only in PostgreSQL: a transaction the client opens, a default on the role, and a role with no write privilege. An assistant with a database connection gets one of them, and from the setting’s name you cannot tell which.
I ran each one on PostgreSQL 18.6 and tried to write through it. Two of the three failed in one statement. The third refused every direct write to a table, and then wrote a row anyway, through a function somebody else had left in the database. The error texts below are what the server printed.
Some notes describe how Monghoul does this, because it is the implementation I can describe exactly. The server behaviour is the same for every client.
The setup
One database, shop, with customers and orders. Run these statements on a test database only,
because the deletes that succeed remove rows. Two roles matter:
-- The application's own role. It reads and writes.
CREATE ROLE app LOGIN;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO app;
-- The assistant's role. It reads.
CREATE ROLE assistant LOGIN;
GRANT pg_read_all_data TO assistant;
The first two sections use app, because that is the common shortcut: the assistant gets the
connection string the application already has, and something else is asked to keep it read-only.
Layer one: the client opens a read-only transaction
The client sends BEGIN READ ONLY, runs the assistant’s text, and rolls back. Inside that
transaction the server refuses a long list:
| Statement | What PostgreSQL 18.6 answered |
|---|---|
DELETE FROM orders WHERE id = 5 |
cannot execute DELETE in a read-only transaction |
WITH gone AS (DELETE ... RETURNING id) SELECT * FROM gone |
cannot execute SELECT in a read-only transaction |
SELECT log_access(), a function that inserts |
cannot execute INSERT in a read-only transaction |
SELECT nextval('invoice_no') |
cannot execute nextval() in a read-only transaction |
SELECT ... FOR UPDATE |
cannot execute SELECT FOR UPDATE in a read-only transaction |
EXPLAIN ANALYZE DELETE ... |
cannot execute DELETE in a read-only transaction |
CREATE TEMP TABLE t (x int) |
cannot execute CREATE TABLE in a read-only transaction |
SELECT lo_create(0) |
cannot execute lo_create() in a read-only transaction |
The second row is the one a text filter misses, because the statement starts with WITH and ends
with SELECT. The server does not read the first word. It names the outer statement in the error,
and it refuses the write where it happens.
Three statements were not refused: SELECT pg_advisory_lock(7), NOTIFY jobs, 'x' and
SET statement_timeout = 0. None of them changes a table. The advisory lock was still in
pg_locks after the rollback, and another session can wait on it. The SET removes the time limit
for the rest of the transaction. The server sends the NOTIFY only if the transaction commits.
The transaction itself has two ways out, and both worked.
The text changes the mode. SET TRANSACTION READ WRITE is legal until the first query runs.
BEGIN READ ONLY; -- the client
SET TRANSACTION READ WRITE; -- the assistant's text starts here
DELETE FROM orders WHERE id = 3; -- DELETE 1
After one query the same statement fails with transaction read-write mode must be set before any query. So the order of two statements decides whether the wrapper holds.
The text ends the transaction.
BEGIN READ ONLY; -- the client
COMMIT; DELETE FROM orders WHERE id = 4; -- DELETE 1
The COMMIT closes the read-only transaction and the DELETE runs alone, in a new one that the
server opens read-write. psql sends those as two messages, so I also sent them as one string with
psql -c, and the result was the same. Datadog Security Labs
reported the same flaw
in the reference PostgreSQL MCP server on 21 August 2025: it wrapped each query in a read-only
transaction and accepted more than one statement in the text.
Both exits need a role that can write, and a session whose default is read-write. With the default
of layer two also on, the DELETE after the COMMIT failed. Layer two has its own exit.
A read-only transaction is therefore a guard only when the client also sends one statement at a
time and refuses transaction control in it. That needs a parser, not a search for the word
COMMIT.
Layer two: a read-only default on the role
ALTER ROLE app SET default_transaction_read_only = on;
In each new session of the role, every transaction now starts read-only, and a plain DELETE
answers cannot execute DELETE in a read-only transaction. It looks like a server-side control,
because a superuser set it and the client did nothing.
It is a default. The session changes it:
SET default_transaction_read_only = off;
DELETE FROM orders WHERE id = 1; -- DELETE 1
BEGIN READ WRITE does the same for one transaction. So does
SELECT set_config('default_transaction_read_only', 'off', false), which is a plain SELECT to a
filter that looks for SET. Use this setting to stop a mistake by a person. Do not count it as a
limit on a program that writes its own SQL.
Layer three: a role that cannot write
This is the layer that holds, because no setting in the session gives the role a privilege it does
not have. As assistant:
| Statement | What PostgreSQL 18.6 answered |
|---|---|
INSERT INTO orders ... |
permission denied for table orders |
DELETE FROM orders WHERE status = 'draft' |
permission denied for table orders |
WITH gone AS (DELETE ...) SELECT count(*) FROM gone |
permission denied for table orders |
SELECT * INTO orders_copy FROM orders |
permission denied for schema public |
SELECT id FROM orders WHERE id = 1 FOR UPDATE |
permission denied for table orders |
TRUNCATE orders |
permission denied for table orders |
DROP TABLE orders |
must be owner of table orders |
SELECT nextval('invoice_no') |
permission denied for sequence invoice_no |
BEGIN READ WRITE changes nothing here: the DELETE inside it got the same answer. The role has
the transaction and no privilege to use it. The fourth row is true from PostgreSQL 15. Before that
version PUBLIC could create tables in the public schema, and a database upgraded from 14 can
still have that grant.
pg_read_all_data is a predefined role since PostgreSQL 14. It gives SELECT on every table and
view, including the ones created later. An explicit GRANT SELECT ON ALL TABLES does not: in my
test a table created after the grant answered permission denied for table refunds.
ALTER DEFAULT PRIVILEGES covered the next table, and only a table that the same role creates.
What the read-only role still did
The role had no write privilege on any table. With a fresh database and default privileges, it still did all of this:
| What it ran | What happened | Why |
|---|---|---|
SELECT log_access() |
One row inserted into access_log |
The function is SECURITY DEFINER, and EXECUTE on a new function goes to PUBLIC |
CREATE TEMP TABLE big AS SELECT ... |
A 224 MB table on the server’s disk | TEMPORARY on a database goes to PUBLIC |
SELECT lo_create(0) |
A large object stored in the database | EXECUTE on the function goes to PUBLIC |
SELECT pg_advisory_lock(42) |
A lock that another session can wait on | EXECUTE on the function goes to PUBLIC |
SELECT count(*) FROM orders a, orders b |
Ran until the 2 s timeout I had set. SET statement_timeout = 0 then removed the timeout |
The timeout is a setting of the session |
SELECT email, card_last4 FROM customers |
The rows | The role reads every column |
SELECT rolname, rolpassword FROM pg_authid |
The SCRAM hash of the app role’s password |
pg_read_all_data covers the system catalogs |
\c postgres |
Connected | CONNECT on a database goes to PUBLIC |
The first row is the one to check in your own database. A SECURITY DEFINER function runs with the
rights of its owner, so a SELECT that calls it writes whatever the function writes. The assistant
does not need to be hostile. It needs to find a function called refresh_totals and try it.
Five of the rows have a fix on the server. The revokes from PUBLIC change what your other roles can
do too, so grant each privilege back to the roles that use it before you run these on a shared
server. A monitoring agent or a pooler that connects to postgres stops with the fifth statement.
REVOKE EXECUTE ON FUNCTION log_access() FROM PUBLIC;
ALTER DEFAULT PRIVILEGES FOR ROLE migrator REVOKE EXECUTE ON FUNCTIONS FROM PUBLIC;
REVOKE TEMPORARY ON DATABASE shop FROM PUBLIC;
REVOKE EXECUTE ON FUNCTION lo_create(oid), lo_creat(integer), lo_from_bytea(oid, bytea) FROM PUBLIC;
REVOKE CONNECT ON DATABASE postgres FROM PUBLIC;
REVOKE EXECUTE ON FUNCTION pg_advisory_lock(bigint) FROM PUBLIC;
ALTER ROLE assistant CONNECTION LIMIT 3;
ALTER ROLE assistant SET temp_file_limit = '100MB';
I ran each one. After them the function answers permission denied for function log_access, the
temp table answers permission denied to create temporary tables in database "shop", and a fourth
connection answers too many connections for role "assistant". A sort that needs more than 100 MB
of temporary files answers temporary file size exceeds "temp_file_limit" (102400kB).
Four limits of these fixes are easy to miss:
- Default privileges belong to the role that creates. Without
FOR ROLE, the statement covers only the functions that the role that ran it creates later. In my test a function thatmigratorcreated stayed open to the assistant until I ran the statement formigrator. Run it for each role that creates functions. temp_file_limitdoes not count a temporary table. With the 100 MB limit on the role, the 224 MB temporary table was still created. The revoke of TEMPORARY is the fix for that row.- Each revoke covers one database, and one function. The assistant still holds
CONNECT on
template1and on every other database. The threelo_functions are the ones that create a large object, andpg_advisory_lockhas other forms. - The password hashes have no revoke. A role that holds
pg_read_all_datareads the catalogs. A role with SELECT on named tables does not:analyst, from the section on columns below, gotpermission denied for table pg_authid.
What the server cannot hold for you
Two settings look alike and are not.
temp_file_limit is a setting that a superuser changes. Since PostgreSQL 15 a role with
GRANT SET ON PARAMETER can change it too. The assistant had neither, ran SET temp_file_limit = -1 and got permission denied to set parameter "temp_file_limit". A value on the role holds.
statement_timeout is a setting any session can change. I set 15 seconds on the role, and the
assistant ran SET statement_timeout = 0 and had no limit. The same is true of
default_transaction_read_only, as layer two showed. Stock PostgreSQL has no way to pin these for
one role.
So the time limit is a job for the client. A client that refuses SET must refuse set_config()
too, because the server accepted the function inside a read-only transaction. A client that keeps
its own clock and cancels the query does not depend on either. The connection limit is the
server’s part: a role with CONNECTION LIMIT 3 holds three connections, idle ones included, so it
runs three slow queries at most.
Read-only is not the same as safe to read
Everything the assistant selects goes into a conversation with a model. A role that cannot write
and can read customers.email is read-only and is still the larger risk in most databases.
pg_read_all_data has no exception for a table or a column. I revoked SELECT on customers from
assistant, and it still read email, because the predefined role gives the privilege back. It
does obey row-level security: a table with it on and no policy returned 0 rows.
So a column limit needs a role that does not hold pg_read_all_data. I made a second role,
analyst, with USAGE on the schema and SELECT on all tables, and then granted by column:
REVOKE SELECT ON customers FROM analyst;
GRANT SELECT (id) ON customers TO analyst;
With that grant SELECT count(id) FROM customers returns 1000, and both SELECT email FROM customers and SELECT * FROM customers answer permission denied for table customers. A view
that leaves the columns out is easier for a model to query, because SELECT * works on it. The
role then needs SELECT on the view and none on the table.
What Monghoul does on PostgreSQL
Monghoul’s AI access gives an assistant tools over MCP, and on PostgreSQL the statement goes through these steps before it reaches the server.
- It is parsed, not searched. Monghoul parses the statement into a syntax tree and allows only the statement kinds it names. A data-modifying CTE at any depth makes a SELECT a write.
- A read is a SELECT, an EXPLAIN or a SHOW, and it runs in its own READ ONLY transaction. So the server refuses a function that writes to a table, which no parser can see from the call. The transaction does not refuse an advisory lock, as layer one showed.
- The two ways out of layer one are closed. An assistant’s statement runs alone, and it may not contain SET, DO, CALL, BEGIN or COMMIT. It may not create a function, a procedure, a trigger or a rule.
- A write is a separate path. It follows the permissions you set, and a destructive write or any write to a write-protected connection waits for you to read the statement and approve it. A read-only connection refuses it.
- Scope is checked too. Under a database scope a
dblinkcall is refused, and under a list of some databases a read ofpg_stat_activityis refused, because it shows the queries of the others.
That is layer one with the two exits closed. It is still the client’s guard. The role is the server’s, and it is the one that stays true when the client has a bug.
The short version
- Give the assistant its own role, with SELECT on named tables and columns. Use
pg_read_all_dataonly if the role may read every column and the password hashes. Never the application’s connection string. - Revoke EXECUTE on functions, TEMPORARY and unused CONNECT from PUBLIC, for each role that creates functions and in each database. List your SECURITY DEFINER functions before you trust any of it.
- Set a connection limit and
temp_file_limiton the role. Keep the time limit in the client. - Treat a read-only transaction and
default_transaction_read_onlyas a second line. Ask your tool what it does withCOMMIT; DELETE FROM orders. - Decide what the role may read. That is the question a read-only label never answers.
A physical replica is the strictest form of the write limit, because it accepts no write from any role while it is a standby. It limits neither what the role reads nor the load of a query. I did not test one for this page.