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_onlyenabled.
Diagnostic Queries
Recovery
Steps to resolve 25006:
- Route writes to the primary, not a standby.
- Remove
READ ONLYfrom the transaction or resetdefault_transaction_read_only. - Confirm the target:
SELECT pg_is_in_recovery();returns true on a read-only standby.
Reference: PostgreSQL error codes — Class 25 (Invalid Transaction State).
Thanks — noted. This helps keep the database accurate.