How to Fix Postgres Error 25006 Read Only SQL Transaction

Published
Length
3 min read

Luis Felipe Dias

All posts

The postgres error 25006 read only sql transaction happens when a session tries to write data inside a read-only transaction. You can fix it by routing write queries to the primary node or switching the session transaction mode. Database teams often see this error during failovers or when using read replicas. The official PostgreSQL spec maps state 25006 to ERRCODE_READ_ONLY_SQL_TRANSACTION. This rule stops data changes when the engine expects only read queries. Understanding why the session is

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

The postgres error 25006 read only sql transaction happens when a session tries to write data inside a read-only transaction. You can fix it by routing write queries to the primary node or switching the session transaction mode.

Database teams often see this error during failovers or when using read replicas. The official PostgreSQL spec maps state 25006 to ERRCODE_READ_ONLY_SQL_TRANSACTION. This rule stops data changes when the engine expects only read queries. Understanding why the session is locked to reads helps you fix the issue fast.

What causes postgres error 25006 read only sql transaction

Several setup layers can trigger a postgres error 25006 read only sql transaction in your cluster. The most common cause is sending a write command to a hot standby replica. Replicas only accept read queries by design to stay in sync with the primary node. If an app pool sends an insert or update to a replica, the database blocks it.

Another common cause sits at the user role or session level. Admins often set default transaction modes to read-only for reporting users or analytics tools. Client drivers and connection poolers can also mark a session as read-only before running a query. Even if the user has table write rights, the active transaction mode blocks the change.

Sometimes a failover leaves an app pointing at an old primary node that became a standby. In other cases, a developer starts a read-only transaction block by hand and forgets to close it. Any table change, sequence update, or temporary table creation will fail under state 25006.

How to diagnose and fix the error in production

Start by checking whether the target database node is a primary instance or a read replica. If the app connects to a replica, update your routing rules or connection strings. Send all data changes to the primary node and keep report queries on the replica.

If the node is a primary instance, check the session and role settings next. Look at whether the client driver or ORM marks the transaction as read-only. Many app frameworks use annotations or flags to optimize read queries. Remove the read-only flag if the code needs to write data during that call.

Read-Only Root Filesystem + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Avoid fixing permission or transaction issues by giving broad superuser rights to app roles. Instead, use group roles to manage rights cleanly when you have many users. Grant membership in the group role to each user who needs write rights. For read-only users, the SELECT privilege lets them read specific columns of a table or view.

Securing database roles and default rights

When you separate read and write traffic, you should also review default database rights. In PostgreSQL, the special role name PUBLIC gives a right to every role on the system. By default, PUBLIC gets CONNECT and TEMPORARY on databases, plus EXECUTE on functions and procedures. The object owner can revoke these defaults to keep read-only users from making temp objects.

In PostgreSQL 15 and later, the default setup blocks users from creating objects in the public schema. On older versions or upgraded databases, an admin must revoke CREATE on the public schema from PUBLIC. Pair strict schema rules with clear read and write roles so sessions match their transaction modes. You can also apply data masking to rewrite matched fields like emails or card numbers in query results.

Add query and result controls beside Postgres

Grants and pools govern who logs into PostgreSQL. Yet every query in an open session still runs with that role's full scope. In practice, hoop.dev runs beside your database as a sidecar for your Postgres. It reads plain and prepared queries on the wire with no change to your users or drivers.

When a tool or agent sends SQL, hoop.dev Postgres controls check each statement against your deny rules. An AI Analyzer you configure can also score intent or hold a risky query for human review. Matched sensitive columns in result rows are masked in memory before they reach the caller. Each query and decision is logged per listener, which generates evidence for SOC 2, HIPAA, and GDPR audits. View the binary on GitHub.

Frequently asked questions

Can a user with table write privileges still get error 25006?

Yes, table privileges and transaction modes work separately in PostgreSQL. Even a superuser will get error 25006 if the current session or node is in read-only mode.

Why does a SELECT query trigger a read-only transaction error?

A SELECT query can trigger error 25006 if it calls a volatile function that writes data. Sequences, auditing triggers, or functions that create temp tables also fail in read-only mode.

How do I stop read-only users from creating objects in the public schema?

PostgreSQL 15 and newer versions block public schema creation by default for standard users. On older versions, an admin can revoke the CREATE privilege on the public schema from PUBLIC.

The query runs. The emails stay hidden.