Diagnostic Queries
Symptoms
A data-modifying command was issued in a read-only transaction (or on a read-only/standby server). PostgreSQL raises SQLSTATE 25006 (read_only_sql_transaction).
- A write was attempted in a read-only context.
- Common on hot standbys or with
SET TRANSACTION READ ONLY. - Also occurs with
default_transaction_read_only = on.
What the server log shows
ERROR: cannot execute INSERT in a read-only transaction
Why PostgreSQL raises this — what the manual says
the SET TRANSACTION reference (Parameters):
“The transaction access mode determines whether the transaction is read/write or read-only.”
Read-only transactions (explicit, via default, or on a standby) forbid statements that change data. Attempting an INSERT/UPDATE/DELETE/DDL in that context is rejected with 25006.
Common causes
- Connected to a hot standby (read-only) and attempting a write.
SET TRANSACTION READ ONLYordefault_transaction_read_only = on.- Routing a write to a read replica by mistake.
How to fix it
- Direct writes to the primary, not a standby/read replica.
- Set the transaction read/write:
SET TRANSACTION READ WRITE;where allowed. - Check
default_transaction_read_onlyand connection routing.
Related & next steps
Reference: PostgreSQL 18 — SET TRANSACTION.
Thanks — noted. This helps keep the database accurate.