// PostgreSQL error reference

PostgreSQL 25006: read-only SQL transaction

A write was attempted in a read-only transaction: on a hot-standby replica, with default_transaction_read_only on, or after SET TRANSACTION READ ONLY.

SQLSTATE
25006
Name
read_only_sql_transaction
Category
Invalid Transaction State
Error message
cannot execute INSERT in a read-only transaction

What it means

PostgreSQL refuses INSERT, UPDATE, DELETE, DDL and other writes when the transaction is read-only. That happens when you are connected to a streaming-replication standby (all transactions there are read-only), when default_transaction_read_only is on for the server, database or role, or when the transaction was started READ ONLY.

The message names the command that was refused.

Common causes

  • The connection went to a read replica, for example through a load balancer or a DNS name that points at a standby after a failover.
  • default_transaction_read_only = on set on the database or role, sometimes deliberately for reporting users.
  • BEGIN READ ONLY or SET TRANSACTION READ ONLY earlier in the transaction.
  • A managed database switched to read-only because it ran out of storage.

How to fix it

  1. Check whether you are on a standby and whether read-only is set.

    SELECT pg_is_in_recovery();
    SHOW default_transaction_read_only;
    SHOW transaction_read_only;
  2. Send writes to the primary. With libpq 14 and later, target_session_attrs=read-write in the connection string picks the writable host from a list.

  3. If read-only was set on the role or database by mistake, turn it off.

    ALTER ROLE app SET default_transaction_read_only = off;
    ALTER DATABASE shop SET default_transaction_read_only = off;

Reproduce it safely

BEGIN READ ONLY;
CREATE TEMP TABLE t (id int);  -- ERROR: cannot execute CREATE TABLE in a read-only transaction
ROLLBACK;

Related errors

More invalid transaction state errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.