PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 25006: cannot execute UPDATE in a read-only transaction

The current transaction is read-only, so any statement that writes (including nextval) is refused.

PostgreSQL 17.12
25006: cannot execute UPDATE in a read-only transaction

Jump to a cause

  1. 01The transaction was started with BEGIN READ ONLY
  2. 02default_transaction_read_only is set on the login role
  3. 03nextval() in a read-only session

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
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, status text);
INSERT INTO orders VALUES (1, 'new');
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
BEGIN READ ONLY;
UPDATE orders SET status = 'paid' WHERE id = 1;
ROLLBACK;
✕ ERROR
BEGIN
ERROR:  25006: cannot execute UPDATE in a read-only transaction
ROLLBACK
Checkrun as lab_a
BEGIN READ ONLY;
SHOW transaction_read_only;
ROLLBACK;
OUTPUT
BEGIN
 transaction_read_only
-----------------------
 on
(1 row)

ROLLBACK
After the fixrun as lab_a
BEGIN;
UPDATE orders SET status = 'paid' WHERE id = 1;
COMMIT;
✓ SUCCESS
BEGIN
UPDATE 1
COMMIT

02default_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
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, status text);
OUTPUT
CREATE TABLE
Setuprun as postgres
ALTER ROLE lab_a SET default_transaction_read_only = on;
OUTPUT
ALTER ROLE
Reproducerun as lab_a
INSERT INTO orders VALUES (2, 'new');
✕ ERROR
ERROR:  25006: cannot execute INSERT in a read-only transaction
Checkrun as lab_a
SHOW default_transaction_read_only;
OUTPUT
 default_transaction_read_only
-------------------------------
 on
(1 row)
Checkrun as postgres
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';
OUTPUT
 rolname |             setconfig
---------+------------------------------------
 lab_a   | {default_transaction_read_only=on}
(1 row)
Fixrun as postgres
ALTER ROLE lab_a RESET default_transaction_read_only;
OUTPUT
ALTER ROLE

If the account is meant to be read-only, keep the setting and do the write with another account instead.

After the fixrun as lab_a
INSERT INTO orders VALUES (2, 'new');
✓ SUCCESS
INSERT 0 1

03nextval() 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
Setuprun as lab_a
CREATE SEQUENCE order_seq;
OUTPUT
CREATE SEQUENCE
Reproducerun as lab_a
SET default_transaction_read_only = on;
SELECT nextval('order_seq');
✕ ERROR
SET
ERROR:  25006: cannot execute nextval() in a read-only transaction
Checkrun as lab_a
SET default_transaction_read_only = on;
SHOW transaction_read_only;
OUTPUT
SET
 transaction_read_only
-----------------------
 on
(1 row)
After the fixrun as lab_a
SET default_transaction_read_only = off;
SELECT nextval('order_seq');
✓ SUCCESS
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