How to Configure Pydantic AI Postgres Credentials

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up pydantic ai postgres credentials by creating a database role with limited rights. Then you pass those login details into the agent dependency context at runtime. Many teams start by giving the agent a broad app role for speed. That wide role lets an agent alter tables or read sensitive rows by mistake. Scoping the role down before the agent runs is a core step. How pydantic ai postgres credentials work in practice An AI agent framework uses dependency injection to pass database poo

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 postgres credentials by creating a database role with limited rights. Then you pass those login details into the agent dependency context at runtime. Many teams start by giving the agent a broad app role for speed. That wide role lets an agent alter tables or read sensitive rows by mistake. Scoping the role down before the agent runs is a core step.

How pydantic ai postgres credentials work in practice

An AI agent framework uses dependency injection to pass database pools to tool functions. The app loads the login details from a secret store when it starts. It opens a database pool and hands that pool to the agent tools. Each time the model calls a tool, the tool runs a query.

On the database side, admins build group roles to manage privileges for many users. The agent gets its own user role and joins a read-only group role. Admins grant the SELECT privilege to that group on specific tables or views. PostgreSQL also lets you grant SELECT on specific columns of a table. This column rule stops the role from reading other columns in that table. Note that the SELECT privilege also allows COPY TO on those same objects.

You must also check the default rights given to the special PUBLIC role. By default, PUBLIC gets CONNECT and TEMPORARY rights on databases. It also gets EXECUTE rights on functions and procedures. The object owner can revoke these defaults to lock down the agent role. In PostgreSQL 15 and later, random users cannot create objects in the public schema. On older or upgraded databases, an admin must revoke CREATE on public from PUBLIC.

Why role setup is only the first step

Setting up a strict database role solves who can log in and which objects they touch. It stops an agent from dropping tables or reading blocked secret tables. This native setup is required for any safe database deployment.

Still, static database roles leave an operational gap during live agent runs. A role check happens when the session starts and when a query hits a table. It does not inspect what individual statements do in flight within allowed tables. If an agent has SELECT rights on a customer table, it can pull every row. It can also pull sensitive fields like emails, phone numbers, card numbers, or national IDs. Database roles alone do not rewrite matched fields in results before the model sees them. Also, shared app pools hide which specific user prompt triggered a slow or risky query.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Managing agent database sessions and query safety

Teams running agents in production need control over both the login key and the live query. First, keep the database password out of the agent code and prompt context. Load the key only inside the host app runtime that builds the database pool. Second, set statement timeouts on the agent database role so bad queries fail fast. Third, audit the queries the agent generates so you can trace them back to a user.

When agents query tables with personal data, column grants can break existing SQL views. Many teams instead rely on query inspection that rewrites matched fields in results on the fly. This keeps emails and card numbers out of the model context while queries still run.

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

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

No, the agent should always use its own dedicated database role. Web apps often need write rights that an AI agent should never hold. Separate roles also help you track agent queries in your database logs.

How do I stop a Postgres role from running functions or creating temp tables?

PostgreSQL grants CONNECT, TEMPORARY, and function EXECUTE rights to the PUBLIC role by default. The database owner must revoke those rights from PUBLIC and grant them only to trusted roles. This stops the agent role from running unsafe procedures or filling disk space.

Can column privileges replace data masking for AI agents?

Column privileges work well when the agent never needs to query a restricted column. If a tool runs a broad query across a table, a missing column privilege causes an error. Masking rewrites matched fields in results so the query succeeds without exposing raw phone numbers or emails.

The query runs. The emails stay hidden.