Guide
How do I keep a coding agent from deleting or exposing my production database?
Updated
Short answer
Keep production credentials off the developer laptop, and limit the agent's database access to a dev copy it's allowed to break. Then check every database command and migration against what the session is meant to do before it runs. That way a "make the error go away" fix can't drop a table or switch off row-level security. Deny rules help, but they match text, and the same statement can be written many ways.
The two ways an agent hurts a production database#
The agent holds your credentials and acts with them. Whatever your login can do to the database, the agent can do too, usually without a second step.
There are two ways this goes wrong, and each needs its own controls.
- Destructive:
DROP,TRUNCATE, aDELETEwith no scope, a migration run against prod, or a "reset" that wipes data to fix a schema error. - Exposing: switching off row-level security (RLS) to clear a permissions error, pasting a service key into client code, opening a table to every role, or copying a production dump somewhere it shouldn't go.
The best-known public case of the first is the Replit incident, where an agent deleted a production database during a code freeze. The user's verdict, as The Register reported it: "There is no way to enforce a code freeze in vibe coding apps like Replit. There just isn't."
The freeze was an instruction. Nothing enforced it.
The second way is quieter. An agent hits an RLS error, and the fastest way to clear it is to open the table or reach for the key that bypasses the policy.
Supabase's docs are blunt: its secret and service role keys "are never safe to expose because they bypass RLS". The agent that "fixes" the error isn't attacking anyone. It's finishing the task.
Start with credentials and environments#
Most of the prevention is plain engineering, and it comes before any check built for agents.
- Keep production credentials off developer laptops. If the agent can't reach prod, it can't drop prod.
- Give the agent a dev or staging database with synthetic or masked data, and treat it as disposable.
- Use a least-privilege database role for agent work: no
DROP, noTRUNCATE, no DDL outside the dev schema. - Run migrations against production only from CI, with a human approval. Never from a laptop session.
- Keep service and secret keys in a server-side secret store. Client code gets the publishable key only, with RLS turned on and policies written.
- Keep backups where the agent's credentials can't delete them. See can a coding agent delete backups and logs.
Do only this and you've already removed most of the blast radius.
What the built-in controls do and don't stop#
Set permission rules anyway. In Claude Code, rules are evaluated deny, then ask, then allow, and the first match wins. An ask rule on psql, or a deny on an obvious command, costs nothing.
The limit is that rules match the text of a command, not what it does.
- A deny on
DROP TABLEin apsqlcall doesn't cover the same drop sent through an ORM, a migration file, a Python script or amaketarget. - A rule on
psqldoesn't cover a connection made from code the agent just wrote and ran. - Encoded or assembled statements get past a text match.
- A rule can't tell a harmless
DELETEon a test fixture from the sameDELETEon a live table.
Sandboxes limit reach. If prod can't be reached from inside the sandbox, a bad command fails.
But a sandbox with network access and a prod connection string inside it can still reach prod. Scoped credentials are the strongest of these controls, and they still don't judge intent. For the mechanics, read permissions vs hooks vs sandboxing and why command denylists fail.
Then there's the human approval prompt. In Anthropic's study of 1,053 testers, humans caught 13.6% of dangerous commands, and people approve 93% of prompts anyway. A database command buried in a long session gets the same click as everything else.
The check that catches "make the error go away"#
The fixes that do damage look ordinary. The same statement is fine in one session and a disaster in another.
- Dropping and recreating a table is a normal step on a scratch database. On a live one, it's data loss.
- Adding a policy hardens the database. Disabling RLS "to unblock the query" exposes it.
- A service key in a server function is correct. The same key in a client bundle is a leak.
Only the session's intent tells these apart. A useful check looks at the pending action, follows where its data goes and what it touches, and compares that with what the developer asked for.
"Fix the failing query" doesn't mean "turn off the policy". When rules alone can't settle an action, it should stop and ask a human, with the reason recorded.
Where Harden fits#
Harden Agentic Integrity Foundation (AIF) runs on the developer laptop, outside the agent, at the agent's pre-execution hook. Every command, file edit and push is checked against the session's intent before it runs. Decisions are made locally in milliseconds, and nothing leaves the machine.
Destructive commands are checked against where they actually run. Changes that weaken security, like removing a policy or moving a secret into client code, also get a local model's review in the context of the session. That model can only make a verdict stricter, never weaker.
If the check can't run, the action doesn't run. When Harden blocks an action, the agent gets the reason and can retry it a safe way. One policy covers every agent, and it starts watch-only, so you can see what agents do to your databases before you enforce anything.
It's a layer, not a perimeter. It doesn't replace least privilege, scoped credentials or egress policy, and if an agent already holds a prod credential, you've chosen to carry that risk. Read what a pre-execution check should do for the full mechanism.
A decision rule#
If the agent can reach production data, fix that first. If it can't, check every statement it runs against the work it was asked to do.
FAQ
Related questions
Is a read-only database role enough?
It stops destructive writes, which makes it a strong first step. It doesn't stop the agent reading sensitive rows and sending them somewhere. It also doesn't cover exposure mistakes like a service key committed to client code.
Should agents ever run migrations against production?
Not from a laptop session. Run production migrations from CI with a human approval. Let the agent write and test the migration against a dev copy.
Do Claude Code deny rules stop DROP TABLE?
They can stop a command string that matches. They don't stop the same statement sent through an ORM, a migration file or a script the agent writes, because rules match text, not effect.
How do I see what agents do to databases before I enforce anything?
Run a check in watch-only mode. Harden starts that way and records each decision, so a security team can review what agents attempt before turning on blocking.
Why does an agent disable RLS or use a service key?
It's the fastest way to clear a permissions error, and the agent is trying to finish the task. Keep service keys server-side, and check any change that weakens a policy against what the session was asked to do.