Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

PostgreSQL Error 25006: Read-Only SQL Transaction

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

PostgreSQL error 25006 (read_only_sql_transaction) means your session is in a read-only transaction and you tried to run a write statement. The fix is to end the read-only transaction, reconnect with a read-write session, or change the server or database setting that forced read-only mode. First check whether the transaction was started with SET TRANSACTION READ ONLY, whether the database has default_transaction_read_only enabled, or whether you are connected to a standby server that only accepts reads.

What You'll Need

  • Access to the PostgreSQL client (psql, pgAdmin, or your application's database tool).
  • Credentials with permission to view current settings and, if needed, change database or role defaults.
  • Knowledge of whether the server is a primary or a standby (replica).

Step-by-Step

  1. Confirm the error and the statement. The message usually includes the statement that failed. Note whether it was an INSERT, UPDATE, DELETE, CREATE, or similar write command. This tells you the session attempted a write, not a read.
  2. Check the current transaction mode. In psql, run SHOW transaction_read_only;. If it returns on, the current transaction is read-only. Also run SHOW default_transaction_read_only; to see the session default. If either is on, that explains the error.
  3. End the read-only transaction. If you are inside a transaction block, issue ROLLBACK; or COMMIT; to close it. Then start a new transaction. In psql, you can also use \q to quit and reconnect, which resets session state.
  4. Start a read-write transaction explicitly. After reconnecting, begin with BEGIN READ WRITE; before your write statement. This overrides a session-level read-only default for that transaction, provided the server allows writes.
  5. Check the role setting. Run SHOW default_transaction_read_only; after logging in as the role you use. If it is on, the role may have been altered with ALTER ROLE ... SET default_transaction_read_only = on;. To change it for your session only, run SET default_transaction_read_only = off; and retry. To change it permanently for the role, a superuser or the role owner can run ALTER ROLE your_role SET default_transaction_read_only = off;.
  6. Check the database setting. Connect to the database and run SHOW default_transaction_read_only;. If it is on at the database level, a superuser can run ALTER DATABASE your_db SET default_transaction_read_only = off;. Note that this changes the default for new sessions; existing sessions keep their current setting until they reconnect.
  7. Check whether the server is a standby. Run SELECT pg_is_in_recovery();. If it returns t, the server is a standby and will not accept writes. You must connect to the primary server instead. If you intended to write to the standby, that is not possible; use the primary or promote the standby only if you are certain that is the correct operational step.
  8. Reconnect and retry. After making changes, disconnect and reconnect so the new session picks up updated defaults. Then run your write statement again. If it succeeds, the cause was a session or database default. If it still fails, return to step 1 and check for a transaction-level setting inside your application code.

Common Mistakes

  • Running SET TRANSACTION READ WRITE outside a transaction block. That command only works inside a transaction; outside, it errors or has no effect. Use BEGIN READ WRITE; instead.
  • Assuming a successful SET default_transaction_read_only = off; fixes existing connections. It only affects the current session; other sessions keep their old setting until they reconnect.
  • Forgetting that a connection pooler may reuse sessions with read-only defaults. If your application uses a pool, check the pooler's session settings and whether it resets session state between clients.
  • Trying to write to a standby server. A standby is read-only by design; the correct fix is to connect to the primary, not to change settings on the standby.
  • Ignoring application-level transaction wrappers. Some frameworks start transactions with a read-only flag by default for certain operations. Check your ORM or query builder documentation for how to request a read-write transaction.

Tips

  • Use SHOW transaction_read_only; inside the failing transaction to confirm the mode before changing anything.
  • Prefer fixing the cause at the role or database level rather than adding BEGIN READ WRITE; to every query; explicit transaction modes are easy to forget.
  • When using psql, \conninfo shows the current connection details, which helps confirm you are on the intended server.
  • If you manage multiple environments, label read-only replicas clearly in your connection strings so applications do not accidentally send writes to them.
  • Remember that default_transaction_read_only can be set at the server, database, role, or session level; check each level in that order when diagnosing.

Troubleshooting

If the error persists after ending the transaction and reconnecting, work through these checks in order. First, run SELECT current_setting('transaction_read_only'); inside the transaction that fails; if it returns on, something in the session or transaction set it. Second, check SELECT setting FROM pg_settings WHERE name = 'default_transaction_read_only'; to see the effective value. Third, verify the server role with SELECT pg_is_in_recovery();. Fourth, inspect your application's connection string or ORM configuration for a read-only flag. Fifth, if you use a connection pooler, consult its documentation for session reset behavior. If you cannot change the setting because you lack privileges, ask a superuser or the database owner to adjust the role or database default. If the server is a standby and you need writes, connect to the primary; do not attempt to force writes on a standby.

Frequently Asked Questions

What does error 25006 mean exactly?

It is the SQLSTATE code for read_only_sql_transaction: the current transaction is marked read-only, so write statements are rejected.

Can I override read-only mode for a single transaction?

Yes, if the server is not a standby and you have permission. Start the transaction with BEGIN READ WRITE; and run your write statement inside it.

Why does the error appear only sometimes?

Your session or transaction may inherit a read-only default from the role, database, or a previous SET command. Check the current transaction mode before each write.

Does this error occur on standby servers?

Yes. Standby servers are read-only and reject writes with this error. Connect to the primary server to perform writes.

Will changing the database default affect other users?

It changes the default for new sessions connecting to that database. Existing sessions keep their current setting until they reconnect.

How do I check if my role has read-only set?

Run SHOW default_transaction_read_only; after logging in as that role. If it returns on, the role default is read-only.