How to Configure Pydantic AI CockroachDB Least Privilege
You set up pydantic ai cockroachdb least privilege by creating a tight database role. Then you pass that role into your agent tool setup at startup. Most teams start by reusing a broad admin user to test fast. That habit lets an agent drop tables or read private rows by mistake. A scoped role stops the agent from touching tables outside its job. How pydantic ai cockroachdb least privilege works in practice Pydantic AI runs tools that call outside systems during an agent loop. You pass a datab
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 pydantic ai cockroachdb least privilege by creating a tight database role. Then you pass that role into your agent tool setup at startup. Most teams start by reusing a broad admin user to test fast. That habit lets an agent drop tables or read private rows by mistake. A scoped role stops the agent from touching tables outside its job.
How pydantic ai cockroachdb least privilege works in practice
Pydantic AI runs tools that call outside systems during an agent loop. You pass a database pool into the agent through its dependency object. That pool logs in to CockroachDB using a single database user and role. CockroachDB checks that role against table grants before it runs any query.
To keep rights tight, you first build a role with no default rights. Next, you grant read or write rights only on the needed tables. If the agent only answers questions, grant read rights on those views alone. If the agent must save state, grant insert rights on one log table.
You can also limit which columns the database role is allowed to read. This keeps secret keys or password hashes out of the agent context window. You should also set a statement timeout on that specific database role. A short timeout stops a bad agent query from locking up cluster nodes. The dependency object holds this scoped pool and shares it across tool calls.
What native database roles solve and what keeps the session active
Native roles in CockroachDB are great at blocking access to whole tables. They make sure a read-only agent cannot run a drop or delete command. They also split rights cleanly between your web app and your AI worker. If a prompt trick makes the agent try a write, the database refuses.
Still, database grants only check who logs in and which table they touch. They do not inspect what each query does while it runs in flight. An agent with read rights on a customer table can still pull everything. It can read emails, phone numbers, card numbers, or national IDs in bulk. Native table grants cannot rewrite those fields in query results on the fly.
Least Privilege Principle + AI Agent Security: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Static roles also stay active for the whole life of the worker process. If a tool needs write rights once a week, the role keeps them always. You also lose sight of which user prompt triggered a specific database query. CockroachDB logs show the shared agent user, not the human asking the question. Teams need query checks and field masking to close this runtime gap.
Steps to keep agent database sessions safe
Start by listing every query your agent tools need to run in production. Group these queries into read tasks and write tasks before creating any roles. Create one database user for read tools and another user for write tools. Keep both users out of the default admin group from day one.
Set up your agent dependencies so read tools only get the read pool. Put strict row limits in your tool logic so queries stay small. Test the setup by running the agent with a bad query on purpose. Check the database logs to confirm the role blocks the bad call.
Check database and API queries on the wire
Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.
Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.
Frequently asked questions
Can one Pydantic AI agent use multiple CockroachDB roles?
Yes, you can pass more than one database pool inside your dependency object. Give the read pool to lookup tools and the write pool to update tools.
Do CockroachDB roles stop an agent from reading sensitive columns?
Column grants can block a query that asks for a blocked column name. Still, if the agent needs that table, grants will not mask values inside results.
How do I stop an agent query from overloading CockroachDB?
Set a role statement timeout and a row limit inside your database setup. This forces the database to cancel slow or huge queries right away.
Related guides
In this post