25006
PostgreSQLERRORNotableInvalid Transaction StateHIGH confidence

read only SQL transaction

What this means

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.

Why it happens
  1. 1Issuing a DML or DDL statement inside a READ ONLY transaction
  2. 2Connecting to a standby (replica) server and attempting to write
  3. 3default_transaction_read_only = on in postgresql.conf and the application issues a write
How to reproduce

INSERT in a READ ONLY transaction.

trigger — this will error
trigger — this will error
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.

Remove READ ONLY from the transaction if writes are needed
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.

Sources
Official documentation ↗

Class 25 — Invalid Transaction State

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All PostgreSQL errors