How to Configure Codex Postgres Sandbox

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up a codex postgres sandbox by making a restricted database role for the AI agent. Then you grant that role read permissions only on safe tables and columns. Many teams connect AI coding tools straight to shared test or staging databases. They often reuse an existing app user with broad write rights. When an AI agent uses an open role, a bad query can drop tables. It can also pull live customer data into the model prompt. How to set up a native codex postgres sandbox A safe database s

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.

You set up a codex postgres sandbox by making a restricted database role for the AI agent. Then you grant that role read permissions only on safe tables and columns. Many teams connect AI coding tools straight to shared test or staging databases. They often reuse an existing app user with broad write rights. When an AI agent uses an open role, a bad query can drop tables. It can also pull live customer data into the model prompt.

How to set up a native codex postgres sandbox

A safe database setup starts with native PostgreSQL roles and privileges. Instead of sharing a main app login, you create a dedicated role for the agent. If multiple agents or developers need similar access, you can set up a group role. You then add each individual user to that group role to manage rights in one place.

Next, you must check what the special PUBLIC role allows in your database. By default, PostgreSQL gives the PUBLIC role the right to connect to a database. It also lets every user create temporary tables and run functions or procedures. An admin or object owner can revoke these default rights from PUBLIC at any time. In PostgreSQL 15 and newer, users cannot create new objects in the public schema by default. If you run an older database or upgraded from one, you must revoke that create right yourself.

Once the base role is locked down, you grant the select privilege where needed. PostgreSQL lets you grant select rights on a whole table, view, or materialized view. You can also grant select rights on specific columns of a table. This lets the agent read non-sensitive columns while blocking restricted columns on the same table. Keep in mind that the select privilege also allows the copy operation to export data.

Why native database roles are only half the fix

Setting up a read-only database role solves the problem of accidental writes. The AI agent cannot delete rows, alter schemas, or corrupt tables when it lacks write rights. Column grants also help keep secret fields out of basic table reads. For basic local testing with fake data, native database roles work well.

Still, native database permissions only check who logs in and which objects they touch. They do not inspect what individual queries do while running in flight. Once a role has select access to a column, it can read every row inside it by default. If a table holds emails, phone numbers, card numbers, or national IDs, the agent reads them all. The raw values go straight back to the AI tool and into the model context.

AI Sandbox Environments + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static database roles also require you to put a password or key on the client machine. Anyone or any script in that workspace can reuse that database login string. Plus, standard database setups do not pause a risky command to ask a human for approval. If an agent runs a huge export using its select rights, the database simply runs it.

What happens when AI agents run queries in daily work

AI coding agents work fast and often run multiple shell commands or database queries in seconds. When an agent hits an error, it tries new queries on its own to fix the problem. In a shared staging database, a loop of heavy read queries can slow down the server. Without row limits or query review, even a read-only role can cause an outage.

Teams also struggle to track which human user triggered a specific agent query. When ten developers share one read-only sandbox role, the database log shows only that single role name. Security teams cannot tell which developer or coding session ran a sensitive data lookup. They also have to rotate shared passwords across every developer laptop when a key leaks. Moving past static credentials and table grants is key for safe AI database work.

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 sits inline in the wire protocol beside your database as a sidecar for your Postgres. For every plain or prepared query an agent or human runs against production, hoop.dev decides to pass, mask, block, or escalate it for review.

When a tool or agent sends SQL, hoop.dev Postgres controls block banned statements before execution or hold risky ones for human approval via an AI Analyzer you configure. 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 PostgreSQL restrict an AI agent to specific table columns?

Yes, PostgreSQL lets you grant the select privilege on specific columns of a table or view. The database will reject any query from that role that tries to read other columns.

Does a new PostgreSQL role get any permissions by default?

Yes, every new role inherits privileges granted to the special PUBLIC role. By default, PUBLIC allows database connections, temporary table creation, and function execution until you revoke them.

Why do read-only roles still expose sensitive data to AI models?

A read-only role stops data changes, but it still returns raw text from allowed columns. If those columns hold emails, phone numbers, or card numbers, the AI agent receives the unmasked values.

The query runs. The emails stay hidden.