How to Configure Pydantic AI CockroachDB Credentials

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai cockroachdb credentials by passing a database pool into your agent dependencies. You must also create a restricted SQL role in your cluster for the agent. Many teams start by giving the agent a shared root login during early tests. This fast shortcut creates big risks when that agent runs in production. A broad role lets a confused model read or drop tables across the whole database. How to set up pydantic ai cockroachdb credentials First, you need to create a dedicated

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 credentials by passing a database pool into your agent dependencies. You must also create a restricted SQL role in your cluster for the agent. Many teams start by giving the agent a shared root login during early tests. This fast shortcut creates big risks when that agent runs in production. A broad role lets a confused model read or drop tables across the whole database.

How to set up pydantic ai cockroachdb credentials

First, you need to create a dedicated SQL user inside your CockroachDB cluster. Do not reuse the admin user that runs your schema migrations. Give this new user a narrow role with read privileges on specific tables only. If the agent must write data, allow inserts on one target table.

Next, choose how the database checks the identity of your agent app. CockroachDB supports password logins and client TLS certificates for user checks. Client certificates work well for machine workloads because you avoid plain passwords. You load the root certificate and client keys into the host where the agent runs.

Then, you build the database pool inside your Python app startup script. Because CockroachDB speaks the PostgreSQL wire protocol, standard async Postgres drivers work right away. Your startup code reads the database host, user, and secret from local environment variables. It opens a shared pool of database sessions before the agent takes any user requests.

Finally, you wire that pool into the Pydantic AI dependency system. Pydantic AI lets you define a typed dependency object for every agent run. You pass the active database pool inside this dependency object when you call the agent. The agent tools then pull a session from the dependency context to run their queries. This pattern keeps secrets out of prompt text and model weights.

What native database roles solve and what they miss

Native database roles and dependency injection solve the first layer of agent safety. They keep raw secrets out of source code and model prompts. They also block the agent from touching tables outside its assigned schema. If a prompt trick asks for billing tables, the database rejects the query right away.

Still, static database roles only check who logs in at the start of a session. They do not inspect what individual SQL statements do while the agent runs. If an agent needs access to a user table, it can query every column inside it by default. A single bad tool call can pull thousands of rows into the model context.

Ephemeral Credentials + AI Agent Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Static roles also cannot mask sensitive columns like emails, phone numbers, or card numbers on the fly. The database sends raw text back to the agent tool. That raw data then flows straight to the external model provider. Also, database logs only show the shared agent user for every query. When an incident happens, your team cannot link a bad query to a specific end user.

Why agent workloads strain static secrets

Human users run predictable queries through fixed app forms. AI agents write or pick queries dynamically based on unpredictable user prompts. A static secret stays valid all day and trusts every query from that app. If an attacker tricks the agent, the database treats the bad query as valid work.

Rotating client certificates or passwords across many agent workers also takes manual effort. Teams often skip rotation because they fear breaking active agent jobs. When a secret leaks in a crash log, it stays active until someone notices. Safe agent setups need controls that act on each query, not just the initial login.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

Can Pydantic AI use standard Postgres drivers for CockroachDB?

Yes, CockroachDB uses the standard PostgreSQL wire protocol for client traffic. You can pass any async or sync Postgres pool into your agent dependencies.

Should an AI agent share a database role with the main web app?

No, you should always give the agent its own separate database user and role. This separation lets you limit table grants and track agent queries in your logs.

How do you stop an agent from reading sensitive columns in allowed tables?

Native table grants allow access to the whole table or specific columns at rest. To hide emails or card numbers inside allowed columns, you must rewrite results in flight.

The query runs. The emails stay hidden.