SQLSTATE 25006 ERROR Class 25: Invalid Transaction State

read_only_sql_transaction cannot execute … in a read-only transaction — 25006

PostgreSQL error “cannot execute … in a read-only transaction — 25006” (SQLSTATE 25006): what it means, common causes, and how to fix it.

PG 9.6, 10, 11, 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed Jun 2026 Grounded in source

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 ONLY or default_transaction_read_only = on.
  • Routing a write to a read replica by mistake.

How to fix it

  1. Direct writes to the primary, not a standby/read replica.
  2. Set the transaction read/write: SET TRANSACTION READ WRITE; where allowed.
  3. Check default_transaction_read_only and connection routing.

Related & next steps

Reference: PostgreSQL 18 — SET TRANSACTION.

Was this helpful?