← Vibe coding database safety

How to keep AI coding agents away from your production database

Updated · DBShifts · commands tested on real PostgreSQL, MySQL and MongoDB servers

Short answer

Give the agent a database it is allowed to break. Keep production credentials out of every file the agent can read, give it a local or branch database with production's schema, and if it truly needs production, a read-only account with a statement timeout. Turn on command approval in your AI tool, and keep a backup you have restored for the day the rest fails.

Key facts

  • Coding agents read your project files, .env included. Whatever DATABASE_URL is there is the database they connect to.
  • A read-only account stops DROP, DELETE and UPDATE in the database itself, whatever the agent decides to run and whichever tool it runs it from.
  • Read-only does not stop reading. An agent with SELECT on a customers table can copy every email into its context, and from there into a model provider's request logs.
  • A database branch (Neon, Supabase branching) or a local Docker copy gives the agent production's schema without production's customers.
  • Command approval in the AI tool is a speed bump, not a wall: an agent can write a script that connects directly. The database account is the wall.

1. Take production out of the agent's reach

Keep production credentials only in your host's environment settings (Vercel, Railway, Fly, Render and the rest all have one), not in a file in the project. The .env the agent can read should point at a development database. If you must keep a production file locally, keep it outside the project folder the agent works in.

2. Give it a database it can wreck

A local PostgreSQL in Docker takes a minute. Copy production's schema into it — tables, columns, constraints, no rows — so the agent's queries and migrations behave the way they will in production.

ShellA throwaway PostgreSQL for the agent
docker run -d --name dev-postgres -e POSTGRES_PASSWORD=dev -p 5433:5432 postgres:16
ShellCopy production's schema, not its rows, into a development database
pg_dump --schema-only --no-owner "$PROD_DATABASE_URL" | psql "$DEV_DATABASE_URL"

3. If it must read production: a read-only account

Create a separate account for the agent that can only read, cannot hold a query for minutes, and cannot open many connections. Give the agent this account's URL, never the app's.

In PostgreSQL, ALTER DEFAULT PRIVILEGES covers tables created later by the role that runs it; run it as the role that owns your tables.

SQLPostgreSQL: a read-only role for an AI agent
CREATE ROLE ai_agent LOGIN PASSWORD 'use-a-long-random-password';
GRANT CONNECT ON DATABASE app TO ai_agent;
GRANT USAGE ON SCHEMA public TO ai_agent;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO ai_agent;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO ai_agent;
ALTER ROLE ai_agent SET default_transaction_read_only = on;
ALTER ROLE ai_agent SET statement_timeout = '5s';
SQLMySQL: a read-only user for an AI agent
CREATE USER 'ai_agent'@'%' IDENTIFIED BY 'use-a-long-random-password';
GRANT SELECT ON app.* TO 'ai_agent'@'%';
ALTER USER 'ai_agent'@'%' WITH MAX_USER_CONNECTIONS 5;

4. Make your AI tool ask before database commands

Turn off automatic command execution for workspaces that can reach a real database, and deny the commands that wipe data: prisma migrate reset, drizzle-kit push, rails db:reset, dropdb. The next guide has the settings for each tool.

5. Put a gateway between agents and real data

When an agent needs real rows (support, analytics, an internal tool), connect it through a gateway rather than a database account: a revocable token, only the tables you allow, sensitive columns masked before they leave, every request logged, and a switch that stops it.

6. Keep a backup you have actually restored

Everything above reduces the chance; a restored backup decides how bad the day is. Back up, restore into a scratch database, count the rows.

Common mistakes

One DATABASE_URL for everything

The app, the migrations and the agent share one all-powerful account. Give each its own, with only what it needs.

Read-only in the app, not the database

A readonly flag in your ORM config binds your code, not an agent that runs psql. Grants in the database bind everyone.

Telling the agent not to touch production

An instruction in a prompt is advice. The agent can forget it, or be talked out of it by text it reads. Permissions do not forget.

A full production copy as the dev database

It puts real customers' data in a less protected place. Copy the schema, and generate or mask the rows.

Forgetting the migrations folder

An agent that writes migrations changes production the next time you deploy. Read every generated migration before it ships.

How DBShifts helps

  • Airlock connects agents (over MCP or REST) with scoped tokens instead of database accounts: only the tables and columns you allow, personal data masked on the server, every request logged.
  • A kill switch stops every token on a gateway at once, and no API key or AI tool can loosen those rules — only a person signed in can.
  • The free read-only user generator writes the exact grants for PostgreSQL, MySQL, MariaDB, SQL Server, Oracle and MongoDB.

Questions people ask

Should I give Cursor or Claude Code my production database URL?

No. Give it a development database, or a read-only production account if it needs real data. The agent can run anything the URL's account is allowed to run.

Is a read-only database user enough for an AI agent?

It stops writes and schema changes. It does not stop reading sensitive columns or running expensive queries, so add a statement timeout, a connection limit, and masking for personal data.

What is a database branch?

A copy of a database's schema (and, on some platforms, its data) that you can change and throw away. Neon and Supabase offer branches; a Docker container with a schema-only copy does the same locally.

Give agents data, not your database password

Read-only by default, masked, logged, revocable. Free to start.

7 guides in this series. See the checklist.