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
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 = onset on the database or role, sometimes deliberately for reporting users.BEGIN READ ONLYorSET TRANSACTION READ ONLYearlier in the transaction.- A managed database switched to read-only because it ran out of storage.
How to fix it
-
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;
-
Send writes to the primary. With libpq 14 and later,
target_session_attrs=read-writein the connection string picks the writable host from a list. -
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
42501permission denied57P03cannot connect now25001cannot run inside a transaction block1792MySQL: Write in a READ ONLY transaction1290MySQL: Server option prevents this statementSQLITE_READONLYSQLite: attempt to write a readonly database
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.
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.