SQLSTATE 25006 ERROR Class 25: Invalid Transaction State

read_only_sql_transaction Read Only Sql Transaction — SQLSTATE 25006

A write was attempted in a read-only transaction or on a read-only server.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

Symptoms

A write was attempted in a read-only transaction or on a read-only server.

  • The error is written to the server log and returned to the client carrying SQLSTATE 25006.
  • Any driver (libpq, JDBC, psycopg, npgsql, pgx) surfaces this code in its error object so you can branch on it programmatically.
  • PL/pgSQL can trap it by name: EXCEPTION WHEN read_only_sql_transaction THEN.

Environment

Severity: ERROR  |  PostgreSQL versions: 12, 13, 14, 15, 16, 17

Reproduce with the exact statement and read the full message in the server log (raise log_min_messages / set log_min_error_statement for more context).

Root Cause

The session is read-only — from SET TRANSACTION READ ONLY, default_transaction_read_only, or because the server is a hot standby/replica.

Common causes:

  • Writing to a replica.
  • A connection routed to a standby.
  • A read-only transaction setting.
  • default_transaction_read_only enabled.

Diagnostic Queries

Recovery

Steps to resolve 25006:

  1. Route writes to the primary, not a standby.
  2. Remove READ ONLY from the transaction or reset default_transaction_read_only.
  3. Confirm the target: SELECT pg_is_in_recovery(); returns true on a read-only standby.

Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).

Was this helpful?