Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
SQLSTATE 25006 names the statement that was refused: UPDATE, INSERT, or even nextval(). The data and your grants are fine; the transaction itself is marked read-only. Check it with SHOW transaction_read_only, which returns on.
In the runs below the read-only flag came from BEGIN READ ONLY, from ALTER ROLE ... SET default_transaction_read_only = on, and from setting the same parameter in the session.
01The transaction was started with BEGIN READ ONLY
BEGIN READ ONLY; followed by UPDATE orders SET status = 'paid' WHERE id = 1; failed with 25006: cannot execute UPDATE in a read-only transaction. Inside such a transaction SHOW transaction_read_only returned on.
The same UPDATE in a plain BEGIN; ... COMMIT; returned UPDATE 1.
Setup
CREATE TABLE orders (id int PRIMARY KEY, status text);
INSERT INTO orders VALUES (1, 'new');CREATE TABLE
INSERT 0 1BEGIN READ ONLY;
UPDATE orders SET status = 'paid' WHERE id = 1;
ROLLBACK;BEGIN
ERROR: 25006: cannot execute UPDATE in a read-only transaction
ROLLBACKBEGIN READ ONLY;
SHOW transaction_read_only;
ROLLBACK;BEGIN
transaction_read_only
-----------------------
on
(1 row)
ROLLBACKBEGIN;
UPDATE orders SET status = 'paid' WHERE id = 1;
COMMIT;BEGIN
UPDATE 1
COMMIT02default_transaction_read_only is set on the login role
The role had ALTER ROLE lab_a SET default_transaction_read_only = on, which is a typical setup for a reporting account. A plain INSERT INTO orders VALUES (2, 'new') then failed with cannot execute INSERT in a read-only transaction, with no BEGIN in sight.
SHOW default_transaction_read_only returned on, and pg_db_role_setting showed {default_transaction_read_only=on} for lab_a. After ALTER ROLE lab_a RESET default_transaction_read_only; (as postgres) a new session could insert.
Setup
CREATE TABLE orders (id int PRIMARY KEY, status text);CREATE TABLEALTER ROLE lab_a SET default_transaction_read_only = on;ALTER ROLEINSERT INTO orders VALUES (2, 'new');ERROR: 25006: cannot execute INSERT in a read-only transactionSHOW default_transaction_read_only; default_transaction_read_only
-------------------------------
on
(1 row)SELECT r.rolname, s.setconfig FROM pg_db_role_setting s JOIN pg_roles r ON r.oid = s.setrole WHERE r.rolname = 'lab_a'; rolname | setconfig
---------+------------------------------------
lab_a | {default_transaction_read_only=on}
(1 row)ALTER ROLE lab_a RESET default_transaction_read_only;ALTER ROLEIf the account is meant to be read-only, keep the setting and do the write with another account instead.
INSERT INTO orders VALUES (2, 'new');INSERT 0 103nextval() in a read-only session
With default_transaction_read_only = on, SELECT nextval('order_seq') failed with cannot execute nextval() in a read-only transaction. A SELECT that calls nextval still changes the sequence, so it counts as a write.
After SET default_transaction_read_only = off; in the session, nextval returned 1.
Setup
CREATE SEQUENCE order_seq;CREATE SEQUENCESET default_transaction_read_only = on;
SELECT nextval('order_seq');SET
ERROR: 25006: cannot execute nextval() in a read-only transactionSET default_transaction_read_only = on;
SHOW transaction_read_only;SET
transaction_read_only
-----------------------
on
(1 row)SET default_transaction_read_only = off;
SELECT nextval('order_seq');SET
nextval
---------
1
(1 row)FAQ
How do I fix cannot execute INSERT in a read-only transaction?
Find where read-only comes from: SHOW transaction_read_only and SHOW default_transaction_read_only. Then start a normal transaction, set the parameter off, or reset it on the role, as in the three cases above.
I never wrote BEGIN READ ONLY. Why is my transaction read-only?
The default can be set on the role. In the second case pg_db_role_setting showed default_transaction_read_only=on for lab_a.
Why does SELECT nextval() fail in a read-only transaction?
nextval changes the sequence. PostgreSQL refused it with 25006 just like an INSERT.
How do I check if the current transaction is read-only?
Run SHOW transaction_read_only;. It returned on inside BEGIN READ ONLY and when default_transaction_read_only was on.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12