How to Configure Pydantic AI MariaDB Permissions
You set up pydantic ai mariadb permissions by making a dedicated database user. Then you pass that user key into your agent tool setup. Most teams start by giving their agent a broad admin role during tests. That shortcut creates a huge risk when the app moves to production. An agent with broad rights can drop tables or read private user rows. You need strict database rules before your agent runs its first live query. How pydantic ai mariadb permissions work in practice Your agent does not ta
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 mariadb permissions by making a dedicated database user. Then you pass that user key into your agent tool setup. Most teams start by giving their agent a broad admin role during tests. That shortcut creates a huge risk when the app moves to production. An agent with broad rights can drop tables or read private user rows. You need strict database rules before your agent runs its first live query.
How pydantic ai mariadb permissions work in practice
Your agent does not talk to the database on its own. Instead, you write a Python function and register it as an agent tool. The agent decides when to call that function based on user prompts. Your Python code opens the database link and runs the query. MariaDB checks the user role tied to that link before it runs anything.
A good setup starts inside the database itself. You create a new database role just for your AI agent. You grant that role read access on the exact tables the agent needs. If the agent only needs three columns, you grant read rights on those columns. You can also build a database view that filters out private rows first. Then you grant the agent role read access to that safe view only. Next, you store the database user key in your app secret store. Your Python app reads the secret at startup and builds a connection pool. You pass that pool into the agent context so tools can share it.
Why static database roles leave a runtime gap
Native database roles do a great job at the gate. They control which user logs in and which tables that user can touch. They also block write queries when you only grant read rights. This stops an agent from deleting rows or changing table structures by mistake.
Still, static roles cannot inspect what an individual query does in flight. Once MariaDB lets the user log in, the role allows any valid read query. A confused agent might run a massive join that locks up your server. The database role will allow that bad query because the table rights match. Static roles also treat every column read the same way by default. If a table holds emails, phone numbers, or card numbers, the query pulls them. Your Python tool then sends those raw values straight into the model prompt. Also, shared app roles hide who asked the agent to run the query. The database log only shows the single service user name on every line.
AI Agent Permissions: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Best practices for safer agent database tools
You can close part of this gap inside your tool functions. Do not let the model write raw SQL strings on its own. Instead, build fixed query templates inside your Python functions. Let the model pass typed arguments that your code validates with schemas. This stops prompt tricks from turning a simple lookup into a full table dump.
You should also set query time limits on the database user account. MariaDB lets you cap how long a query can run before it stops. You can also limit how many links the agent account opens at once. These simple caps keep a loop in your agent from crashing the database. Even with these steps, your team still needs visibility into live queries. You also need a reliable way to rewrite sensitive fields in query results.
Inspect every MySQL query and response in flight
Static grants and network rules block bad logins. They cannot pause a risky query for review or mask emails in a result set. By design, hoop.dev is an open-source proxy that runs as a sidecar for your MySQL. It reads plain queries, prepared statements, and cursors on the wire.
First, hoop.dev guardrails block banned actions before MySQL runs them. An AI Analyzer you configure can score intent, and hoop.dev data masking rewrites sensitive string columns in results. Every query and verdict goes to the resource audit trail. Find the open-source binary on GitHub.
Frequently asked questions
Should my AI agent use the main app database role?
No, you should never share the main app role with an AI agent. The main app needs write access, while most agents only need read access. A separate role limits damage if a prompt tricks the agent.
How do I stop an agent from reading sensitive columns?
You can grant column-level read rights or use a filtered view in MariaDB. You can also use a runtime control that rewrites emails or card numbers in results.
Can I let Pydantic AI generate raw SQL queries?
You can, but doing so carries high risk for data leaks and slow queries. Give the agent strict tool functions with typed inputs and fixed SQL templates instead.
Related guides
In this post