⏱️ Reading time: 8 min
When an AI agent like Claude Code investigates why an order didn’t ship, finding the code that processes orders isn’t enough: it needs to see that order’s actual state in the database. Handing it the administrator’s credentials is the fast way out, and also the riskiest. PgStack, a session proxy for PostgreSQL, published a guide on November 30, 2026 with native PostgreSQL techniques so that agent can work with temporary PostgreSQL credentials, read-only access and every query logged [source: PgStack].
📑 En este artículo
- What an AI agent actually needs to debug production
- Read-only roles: the first filter in PostgreSQL
- Temporary PostgreSQL credentials: why expiring the password isn’t enough
- Read replicas and session proxies: spreading load and automating shutdown
- Four ways to limit an AI agent in PostgreSQL
- What this means for teams integrating AI agents into production
- What this doesn’t solve
- Frequently Asked Questions
- References
What an AI agent actually needs to debug production
The scenario PgStack describes is common: a customer reports their order never arrived, the agent finds in the code which states block shipping, but to know which one applies it needs that order’s current state [source: PgStack]. There are two paths: a human runs the query by hand and passes the result to the agent, or the agent connects directly to the database.
The second option saves time, but shifts the security problem from reading code to reading, and potentially writing, production data. A backend developer or SRE integrating Claude Code, Codex or a similar agent into their on-call workflow needs to decide what permissions that connection runs with, not just whether to allow it.
Many teams already solved this for humans: an on-call engineer got a read-only user, scoped and with an expiration date. The problem is that when connecting an AI agent to the same pattern, most teams reuse the application’s user without thinking, which usually has full write permissions.
Read-only roles: the first filter in PostgreSQL
PostgreSQL offers two mechanisms to block writes. The first is read-only transactions, which can be set when starting a transaction or as a role’s default via default_transaction_read_only. The problem is that a role with write permission can turn that setting off within the same session [source: PgStack].
The second, more reliable mechanism is creating a dedicated role with no login privilege and granting it SELECT only on the tables the agent needs, for example the orders table and nothing else. A connection user that inherits that role is then created for a specific investigation. PostgreSQL enforces these permissions regardless of transaction mode, according to its official documentation.
There’s a detail PgStack’s guide flags as a warning worth reading twice: PostgreSQL permissions are additive. If someone at some point granted UPDATE on the orders table to the PUBLIC role (which every user inherits), the agent’s read-only role can still end up writing, because that general permission stacks with its own [source: PgStack]. Auditing what PUBLIC has on every sensitive table matters as much as designing the agent’s role.
Temporary PostgreSQL credentials: why expiring the password isn’t enough
PostgreSQL lets you set an expiration date for a role’s password with the VALID UNTIL clause. Past that date, the server rejects any new connection attempt using that password, according to the CREATE ROLE documentation.
The catch, which PgStack documents with a reproducible test, is that connections already open before expiration keep working. An agent that opened its session minutes earlier can keep querying the database long after its password expired [source: PgStack].
Actually cutting off access takes an extra step: switching the role to NOLOGIN, manually terminating open connections with pg_terminate_backend, and only then dropping the role. In production that means automating credential creation, scheduling cleanup, retrying on failure, and verifying connections actually closed, not just trusting the expiration clock.
Read replicas and session proxies: spreading load and automating shutdown
A PostgreSQL replica in standby mode rejects any write, even if the agent’s role has write permission on the primary node. The role still decides which tables and rows it can see, but the replica adds another layer: no write command ever executes there [source: PgStack]. Running the agent’s queries against the replica also takes that CPU and memory load off the primary node.
The cost is replication lag. If the replica falls behind, an agent investigating why a payment isn’t showing up may see the order as unpaid even though the payment already processed on the primary. For an on-call investigation, that margin of error matters.
The session proxy, which is what PgStack sells as a product, combines these pieces into one layer: it generates read-only credentials that expire on their own when the session closes and logs every SQL query the agent ran during that period [source: PgStack]. It doesn’t replace PostgreSQL’s permissions, it automates them.
flowchart TD
A["AI Agent"] --> B["Session proxy with temporary credential"]
B --> C["Read-only role in PostgreSQL"]
C --> D[("Production database")]
C --> E["Query log (log_statement)"]
Four ways to limit an AI agent in PostgreSQL
None of the four techniques PgStack describes solves everything on its own; it helps to see them as layers that combine, not mutually exclusive options.
| Technique | What it blocks | Main limitation |
|---|---|---|
Read-only transaction (BEGIN READ ONLY) | Writes within that transaction | A role with write permission can disable it within the same session |
Dedicated read-only role (GRANT SELECT) | Any write, regardless of transaction mode | Permissions are additive: a GRANT to PUBLIC can override it |
| Read replica in standby mode | Every write, even if the role has write permission | Can lag behind the primary database |
Temporary credentials (VALID UNTIL) plus session proxy | New connections after expiration | Already-open connections survive; they must be closed manually or through automation |
What this means for teams integrating AI agents into production
If your team already lets Claude Code or Codex review production incidents, the concrete question is which database user those sessions run as. If they currently share the application’s user, that’s the first change to make: a separate role, with no login of its own, with SELECT scoped to the tables the investigation actually touches.
Before the next on-call shift, define three things: who creates and deletes those temporary roles, and with what automation, not by hand on a Friday night; whether the agent’s queries run against the primary or a replica; and who reviews the log_statement log once the investigation closes. Without those three answers, even a well-designed read-only role stays open longer than anyone remembers.
For a DBA or SRE, the cheapest starting point is auditing what permissions PUBLIC currently has on the tables an agent might touch. It’s the step PgStack flags as the one most people overlook, and it doesn’t require installing anything new.
The reverse question is worth asking before adding a third-party proxy: a dedicated read-only role, VALID UNTIL with manual connection termination, and logging with log_statement are the first three techniques in this piece, and all three are native to PostgreSQL, with no extra infrastructure. A team with occasional on-call shifts can cover the whole use case with just that, automating role creation and deletion with its own script; a session proxy starts to make sense once the volume of simultaneous investigations makes that manual automation heavy.
What this doesn’t solve
Restricting writes doesn’t close every risk. A read-only user can still read personal information, authentication tokens or other customers’ data if the tables aren’t separated by columns, views or row-level security [source: PgStack].
An unbounded SELECT query can scan huge tables, consume memory and CPU, or hold locks for a long time; complex row-level security policies add extra overhead to every query. And the agent’s credentials, even if temporary, are still a secret: if they leak, they grant access, even if scoped and time-limited.
This isn’t a problem solved once. Every new table the agent needs to query reopens the same decision about permissions, visible columns and credential lifetime.
Frequently Asked Questions
Is a read-only role in PostgreSQL enough to pass a security audit?
Not on its own. It covers the agent not writing, but an audit also asks what it queried and when, which is what query logging with log_statement or a session proxy that automates it provides [source: PgStack].
What if the AI agent needs to fix data, not just read it?
That write should go through a different path than the investigation: a separate role, with explicit permissions for that specific operation and, if possible, a human confirming the change before it runs. Mixing exploratory reads with write permission in the same role is exactly what these techniques aim to avoid.
References
- AI Agents Shouldn’t Have Permanent Database Credentials, PgStack: the original guide covering the role, temporary credential and session proxy techniques.
- PostgreSQL: CREATE ROLE: official syntax for
VALID UNTILand role inheritance withNOLOGIN. - PostgreSQL: Row Security Policies: official documentation of row-level security.
- PostgreSQL: BEGIN: official documentation of the read-only transaction mode.
Featured image: Foto de FlyD en Unsplash
How each article is made (sources, testing and editorial criteria): How we work.
Did it work for you? Got a different error? Say so below: questions get answered and help the next reader.
Leave a comment
0 Comments