PG
PRO
25006ERRORTier 2 — Caution✅ HIGH confidence

read only SQL transaction

Category: Invalid Transaction StateVersions: All Postgres versions

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
BEGIN READ ONLY;
INSERT INTO orders (total) VALUES (100); -- write in READ ONLY tx
ERROR: cannot execute INSERT in a read-only transaction

Fix 1: Remove READ ONLY from the transaction if writes are needed

When the application should write and the READ ONLY was set unnecessarily.

fix
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 connecting to a standby replica that is read-only.

fix

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 docs: https://www.postgresql.org/docs/current/errcodes-appendix.html

🔧 Source ref: Class 25 — Invalid Transaction State

Confidence assessment

✅ HIGH confidence

Standard SQLSTATE. Stable across all versions.

See also

📄 Reference pages

Transaction Access ModeRead-Only Standbydefault_transaction_read_only
⚙️ This error reference was generated with AI assistance and reviewed for accuracy. Examples are provided to illustrate common scenarios and may not cover every case. Always test fixes in a development environment before applying to production. Spotted an error? Suggest a correction →