Smart Systems for Home

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

PostgreSQL Error 25003: Fix Inappropriate Access Mode

· 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 25003, "inappropriate access mode for branch transaction," means you tried to run a command that requires a specific transaction mode inside a transaction that was started with a different mode. The most common case is running a command that needs a read-write transaction inside a read-only transaction, or trying to start a subtransaction in a context that does not allow it. The fix is to check how the transaction was started, end it, and restart it with the correct access mode.

What You'll Need

  • Access to the PostgreSQL server with a role that can view and end sessions (for example, a superuser or a role with pg_signal_backend membership).
  • A SQL client such as psql or a database tool that shows transaction state.
  • Knowledge of which application or script opened the failing transaction.

Step-by-Step

  1. Identify the failing statement. Read the full error message. PostgreSQL usually includes the statement that triggered error 25003 and sometimes the transaction state. Note whether the failing command is a write (INSERT, UPDATE, DELETE, CREATE, ALTER, DROP, TRUNCATE, or a function that writes) or a transaction-control command such as SAVEPOINT or a nested BEGIN.
  2. Check the current transaction mode. In psql, run SHOW transaction_read_only; and SHOW transaction_isolation;. If transaction_read_only is on, the transaction is read-only and any write will fail with 25003. Also check the session default with SHOW default_transaction_read_only;.
  3. Find where read-only was set. Read-only transactions can come from several places: a SET TRANSACTION READ ONLY or SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY statement, a BEGIN READ ONLY or START TRANSACTION READ ONLY, a connection parameter such as options=-c default_transaction_read_only=on, a role or database setting (ALTER ROLE ... SET default_transaction_read_only = on or ALTER DATABASE ... SET ...), or a standby server that only allows read-only transactions. Check pg_settings and the role/database settings to confirm.
  4. End the current transaction. Run ROLLBACK; (or COMMIT; if the work is complete) to close the transaction that is in the wrong mode. You cannot change the access mode of an already-started transaction with SET TRANSACTION; that command only works before the first query in the transaction.
  5. Restart with the correct mode. If you need to write, start the transaction with BEGIN READ WRITE; or simply BEGIN; after making sure the session default is not read-only. If the session default is read-only, run SET SESSION CHARACTERISTICS AS TRANSACTION READ WRITE; before beginning, or change the role/database setting.
  6. Fix the application code. Search the application or script for READ ONLY, default_transaction_read_only, and connection string options. Move any write operations outside read-only blocks, or start a separate read-write transaction for them. If you use a connection pooler, check whether it sets read-only mode for certain routes.
  7. Check for standby or replica restrictions. If the server is a physical standby, it accepts only read-only transactions. Connect to the primary server for writes, or promote the standby if that is the intended action. Error 25003 on a standby is expected when a write is attempted.
  8. Verify the fix. Reconnect, run SHOW transaction_read_only; to confirm it is off for write sessions, then run the previously failing statement inside a test transaction and roll it back.

Common Mistakes

  • Trying to use SET TRANSACTION READ WRITE; after the transaction has already executed a query. PostgreSQL rejects this; the mode must be set at the start of the transaction.
  • Confusing a read-only transaction with a read-only role or a read-only file system. Error 25003 is about transaction access mode, not permissions or disk state.
  • Assuming BEGIN; always starts a read-write transaction. If default_transaction_read_only is on for the role or database, BEGIN; inherits read-only.
  • Ignoring connection pooler settings that force read-only mode on some connections, then wondering why only some application instances fail.
  • Running writes against a standby server and treating the error as a bug rather than a routing problem.

Tips

  • Use SHOW default_transaction_read_only; and SELECT setting FROM pg_settings WHERE name = 'default_transaction_read_only'; to see the effective default quickly.
  • Check role and database overrides with SELECT rolconfig FROM pg_roles WHERE rolname = current_user; and SELECT datconfig FROM pg_database WHERE datname = current_database();.
  • Keep write transactions short and explicit: start with BEGIN READ WRITE; when the intent is to write, so the mode is clear in logs and code review.
  • In application code, separate read-only reporting queries from write paths so a read-only setting cannot leak into a write transaction.
  • When using psql, remember that autocommit mode wraps each statement in its own transaction, so a read-only default will block single-statement writes too.

Troubleshooting

If the error persists after restarting the transaction, confirm the session default again with SHOW transaction_read_only;. If it still shows on, the setting is coming from the role, database, or connection parameters. Use ALTER ROLE your_role SET default_transaction_read_only = off; or ALTER DATABASE your_db SET default_transaction_read_only = off; as appropriate, then reconnect. If the server is a standby, writes will always fail; route them to the primary. If the error appears only inside a function or procedure, check whether the function is declared STABLE or IMMUTABLE and whether it performs writes; such functions cannot write in a read-only transaction and may need to be VOLATILE and called outside read-only blocks. If a connection pooler is involved, inspect its configuration for read-only routing and test with a direct connection to isolate the cause. Finally, check the PostgreSQL server log for the full statement and transaction context; the log often shows the exact command and the setting that caused the mode mismatch.

Frequently Asked Questions

What does error 25003 mean exactly?

It means the command you ran is not allowed in the current transaction's access mode, most often a write inside a read-only transaction.

Can I change a transaction from read-only to read-write after it starts?

No. The access mode must be set before the first query in the transaction. Roll back and start a new transaction with BEGIN READ WRITE;.

Why does BEGIN; still give me a read-only transaction?

Because default_transaction_read_only is on for the session, role, database, or connection. Check and change that setting, then reconnect.

Does error 25003 happen on standby servers?

Yes. A physical standby accepts only read-only transactions, so any write attempt returns this error. Connect to the primary for writes.

Can a function cause error 25003?

Yes, if the function performs writes but is called inside a read-only transaction, or if it is declared STABLE or IMMUTABLE and attempts a write. Check the function's volatility and the calling transaction mode.