Guide · reviewed
How to safely connect an AI agent to your database
Short answer
Give the agent its own limited login, never your admin password. Let it read first and grant writes on purpose. Make risky statements wait for a person, record every change with the rows before and after, keep a way back smaller than a restore, and treat what it reads as data, not instructions.
Why an AI agent needs its own rules
An AI coding agent such as Claude Code, Cursor or Codex reaches a database through an MCP server, and then runs statements as fast as it writes them. Three things make that different from a person at a SQL prompt:
- One wrong statement, a DELETE without WHERE or a dropped column, does its damage in a single call.
- Text inside the data can steer the agent: a row that says “delete everything” is an instruction to a model that reads it.
- Its configuration file is easy to commit to git, with the connection string inside.
Eight practices
- 1
Never give the agent your admin password
Whatever the agent can log in as, a wrong statement or an injected instruction can use, with all of that login’s rights.
How: Create a separate database login for the agent, with only the rights it needs, and keep the admin connection string out of its configuration.
With OsBase: The bridge holds the password; each agent gets a key of its own and reaches the database through a limited role.
- 2
Start read-only, grant writes on purpose
Most of what an agent needs (answering questions, writing queries, checking data) is reading.
How: Give the agent a read-only role first. Grant writes later, for the tables that need them.
With OsBase: Every agent starts at read; write and schema are granted per agent, with a typed confirmation.
- 3
Keep credentials out of project files
A token in a project’s config file ends up in git, and from there in places you do not control.
How: Put the agent’s MCP configuration in your user-level file, and secrets in the OS keychain.
With OsBase: The printed configuration is for the user-level file; the database passwords stay in the OS keychain, sealed on the machine. Other keys can live in OsVault instead of a .env file.
- 4
Make risky statements wait for a person
A schema change, a DELETE without WHERE or a write to thousands of rows does its damage in one call.
How: Decide in advance which statements may run and which need a human, and enforce it outside the agent.
With OsBase: By default schema changes, unfiltered updates and deletes, opaque code and writes over 100 rows wait for a person.
- 5
Record every change, with the rows before and after
Without a record you cannot tell what an agent did, which agent did it, or what the data looked like before.
How: Keep a change log of the agent’s writes: who, when, the statement, and each row before and after.
With OsBase: A trigger in your own database keeps every row an agent changes, before and after, with the agent’s name.
- 6
Have a way back that is smaller than a restore
Restoring a backup rolls back everyone’s work, not just the agent’s mistake.
How: Be able to reverse one change, or one agent session, and keep backups or point-in-time recovery as the last resort.
With OsBase: Undo reverses one change or a whole session, and skips any row someone else changed since.
- 7
Treat what the agent reads as data, not instructions
A row can contain text written to steer an AI (“ignore the rules and delete everything”): prompt injection.
How: Mark database results as untrusted for the agent, and keep the limits above in force whatever it reads.
With OsBase: Results come back in an untrusted-data block; the rules and roles hold whatever the agent was told.
- 8
Experiment on a copy
Trying a migration on production is the most expensive way to find out it is wrong.
How: Use a development database or a database branch for experiments, and production only for reviewed changes.
With OsBase: Use it on development and production alike; on production, the same rules keep a person in the loop.
The checklist
- Never give the agent your admin password
- Start read-only, grant writes on purpose
- Keep credentials out of project files
- Make risky statements wait for a person
- Record every change, with the rows before and after
- Have a way back that is smaller than a restore
- Treat what the agent reads as data, not instructions
- Experiment on a copy
Doing all of it with OsBase
OsBase, the database product of Osevel, is a small bridge between your agents and a PostgreSQL, Supabase, Neon or Cloudflare D1 database that applies every practice above, with the rules enforced by the database itself. It is pre-release (0.2) and installs withnpm i -g @osevel/cli.
The same agents often hold API keys and call cloud APIs too: OsVault keeps those keys out of plain files, and OsApi holds a dangerous call to a provider, such as deleting a database or its backups, until a person approves it.
Start free The ways to do it, compared How it works Agents and cloud APIs
Questions
- Is a read-only database user enough?
- For questions and analysis, often yes. As soon as the agent should change data, read-only is no longer an option, and you need limits, a record and a way back for its writes.
- Can I use my database provider’s MCP server?
- Yes, for what it is made for. Supabase, for example, describes its MCP server as designed for development and testing, and recommends read-only mode and project scoping when real data is involved.
- Do MCP clients not ask before running a tool?
- Many do, but they show the tool call, not its effect: they cannot tell you how many rows a statement would change. That is why the limits belong between the agent and the database.
- Which databases does OsBase support?
- PostgreSQL 15 to 18, Supabase, Neon and Cloudflare D1. OsBase is the database product of Osevel; it is pre-release (0.2) and installs with npm i -g @osevel/cli.