read only SQL transaction
SQLSTATE 25006 is raised when a write operation (INSERT, UPDATE, DELETE, CREATE, etc.) is attempted inside a transaction that was explicitly set to READ ONLY, or on a standby server that does not allow writes.
- 1Issuing a DML or DDL statement inside a READ ONLY transaction
- 2Connecting to a standby (replica) server and attempting to write
- 3default_transaction_read_only = on in postgresql.conf and the application issues a write
INSERT in a READ ONLY transaction.
BEGIN READ ONLY; INSERT INTO orders (total) VALUES (100); -- write in READ ONLY tx
expected output
ERROR: cannot execute INSERT in a read-only transaction
Fix 1
Remove READ ONLY from the transaction if writes are needed
WHEN When the application should write and the READ ONLY was set unnecessarily.
BEGIN; -- or: BEGIN READ WRITE; INSERT INTO orders (total) VALUES (100); COMMIT;
Why this works
Removing READ ONLY allows write operations in the transaction.
Fix 2
Route write traffic to the primary server
WHEN When connecting to a standby replica that is read-only.
Why this works
Use your connection routing logic (e.g., pgBouncer, HAProxy, libpq target_session_attrs=read-write) to direct write queries to the primary.
Class 25 — Invalid Transaction State
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev