# How do I keep an AI agent from deleting a production database?

Give an AI agent a PostgreSQL role that can only read the tables it needs. See the errors DROP, DELETE and TRUNCATE give it and what read-only mode misses.

Source: https://valakes.com/learn/ai-agent-production-database/

Last updated: 2026-11-29

Let the agent connect as a database role that has SELECT on the tables it needs and nothing else, so DROP, DELETE and TRUNCATE fail with a permission error. Keep the production password out of every file and variable the agent can read, give it a copy of the data to work on, and restore a backup once before the agent connects to production.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 29 November 2026.

Give the agent a database role that may only read the tables it needs. PostgreSQL then refuses a `DROP TABLE`, a `DELETE` or a `TRUNCATE` from it, whatever the agent was told and whatever it decides to try. An instruction in the prompt cannot do that, because the agent is free to ignore it.

This is security advice, so the commands below ran on PostgreSQL 16 on the system named in the check line, and the rules follow the PostgreSQL manual. The agent works from its own server account, as in [the article on giving an AI agent SSH access](https://valakes.com/learn/ai-agents-on-servers/), and reaches the database over the local socket. There PostgreSQL knows it by its system user name, so no password appears in this article. How a password gets typed is a separate question, covered in [psql without a password prompt](https://valakes.com/learn/psql-without-password-prompt/).

```console
$ psql --version
psql (PostgreSQL) 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)
```

## Make a role that can only read

The database `shop` belongs to the role `app`. It has three tables, and the agent needs two of them:

```console
$ sudo -u postgres psql -d shop -c '\dt'
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+-------
 public | api_tokens | table | app
 public | customers  | table | app
 public | orders     | table | app
(3 rows)
```

`CREATE ROLE` makes a role that cannot log in unless you add `LOGIN`. A new role has no rights on the tables of someone else, so you add exactly one grant:

```console
$ sudo -u postgres psql -c 'CREATE ROLE agent LOGIN'
CREATE ROLE
$ sudo -u postgres psql -d shop -c 'GRANT SELECT ON orders, customers TO agent'
GRANT
```

## See what the role may not do

Reading works. Then the agent tries the table it has no grant for, and then the three commands that cause the damage:

```console
$ sudo -u agent psql -d shop -c 'SELECT status, count(*) FROM orders GROUP BY status ORDER BY status'
 status  | count
---------+-------
 new     |     1
 paid    |     2
 shipped |     1
(3 rows)
$ sudo -u agent psql -d shop -c 'SELECT * FROM api_tokens'
ERROR:  permission denied for table api_tokens
$ sudo -u agent psql -d shop -c 'DELETE FROM orders'
ERROR:  permission denied for table orders
$ sudo -u agent psql -d shop -c 'TRUNCATE orders'
ERROR:  permission denied for table orders
$ sudo -u agent psql -d shop -c 'DROP TABLE orders'
ERROR:  must be owner of table orders
$ sudo -u agent psql -d shop -c 'CREATE TABLE notes (id int)'
ERROR:  permission denied for schema public
LINE 1: CREATE TABLE notes (id int)
                     ^
$ sudo -u agent psql -d shop -c 'SELECT count(*) FROM orders'
 count
-------
     4
(1 row)
```

`DELETE` and `TRUNCATE` are privileges of the table, and the role has neither. `DROP` is different. It is not a privilege at all. The manual says the right to destroy an object belongs to its owner, so the message is "must be owner" and the fix is never to make the agent the owner, or a member of the role that is. The last command shows that the four rows are still there.

## Start sessions read-only with default_transaction_read_only

You can also make every new session of the role start read-only. Limit it to the production database with `IN DATABASE`, because the next section gives the same role a copy where it may write:

```console
$ sudo -u postgres psql -d shop -c 'ALTER ROLE agent IN DATABASE shop SET default_transaction_read_only = on'
ALTER ROLE
```

The setting takes effect at the next login. To see what it does, pretend that someone grants the role too much by mistake:

```console
$ sudo -u postgres psql -d shop -c 'GRANT UPDATE ON orders TO agent'
GRANT
$ sudo -u agent psql -d shop -c "UPDATE orders SET status = status WHERE id = 1"
ERROR:  cannot execute UPDATE in a read-only transaction
$ sudo -u agent psql -d shop -c 'SET default_transaction_read_only = off' -c "UPDATE orders SET status = status WHERE id = 1"
SET
UPDATE 1
$ sudo -u postgres psql -d shop -c 'REVOKE UPDATE ON orders FROM agent'
REVOKE
```

The first update is refused, even though the grant allows it. The setting catches a grant that is too wide. The manual lists what a read-only transaction disallows: writes to ordinary tables, all `CREATE`, `ALTER` and `DROP` commands, `GRANT`, `REVOKE` and `TRUNCATE`. The second update shows the limit. The setting is a default and not a permission, so the role turns it off for its own session and the write goes through. Only the grants stop an agent that tries.

## Keep the production password out of its reach

A role with SELECT is no help if the agent holds the password of the owner. The usual place for it is a file next to the application:

```console
$ sudo -u agent cat /srv/app/.env
DATABASE_URL=postgres://app:not-a-real-password@localhost/shop
```

An agent that runs `cat` on that file now connects as `app` and can drop anything. The same is true of a password in the environment of the shell that starts the agent, or in a command line that the agent can see with `ps`. [The article on AI agents on servers](https://valakes.com/learn/ai-agents-on-servers/) covers the account side, with [the permissions article](https://valakes.com/learn/file-permissions/) for closing a file like this one. Whatever the form, the agent should hold no copy of the owner's password.

## Give the agent a copy to work on

When the agent needs to try a query that changes things, let it do that on a copy. `pg_dump` makes a consistent copy while the database is in use, and `-T` leaves a table out. Restoring as the agent, into a database it owns, makes the agent the owner of the copy and of nothing else:

```console
$ sudo -u postgres createdb -O agent shop_copy
$ sudo -u postgres pg_dump --no-owner -T api_tokens shop | sudo -u agent psql -q -X -o /dev/null -d shop_copy
$ sudo -u agent psql -d shop_copy -c 'DROP TABLE orders'
DROP TABLE
$ sudo -u agent psql -d shop -c 'SELECT count(*) FROM orders'
 count
-------
     4
(1 row)
```

The agent dropped a table in the copy. Production still has its four orders. The copy holds real rows, so leave out whatever the agent has no use for.

## Restore a backup once before the agent starts

You do not know that a backup works until you have restored it. Make one, restore it into a scratch database and count rows before you need the file:

```console
$ sudo install -d -m 700 -o postgres -g postgres /var/backups/shop
$ sudo -u postgres pg_dump -Fc -f /var/backups/shop/shop.dump shop
$ sudo -u postgres createdb shop_restore_test
$ sudo -u postgres pg_restore -d shop_restore_test --no-owner /var/backups/shop/shop.dump
$ sudo -u postgres psql -d shop_restore_test -c 'SELECT (SELECT count(*) FROM orders) AS orders, (SELECT count(*) FROM customers) AS customers, (SELECT count(*) FROM api_tokens) AS api_tokens'
 orders | customers | api_tokens
--------+-----------+------------
      4 |         3 |          1
(1 row)
$ sudo -u agent ls /var/backups/shop
ls: cannot open directory '/var/backups/shop': Permission denied
$ sudo -u postgres dropdb shop_restore_test
```

The counts match the live tables. The folder is closed to the agent, because a dump holds the whole database, `api_tokens` included. A dump on the same machine does not protect you from losing the machine, so copy it somewhere the agent has no account either.

## Mistakes people make

You point the agent at the connection string the application already uses. It is the owner's, and an owner can drop.

You write `GRANT ALL PRIVILEGES` because the agent "might need it". Grant the privileges one table at a time when it asks.

You count on the prompt. The agent can ignore a line that says "never delete". It cannot get past a grant it does not have.

You forget that every role may connect to every database by default. The manual lists `CONNECT` and `TEMPORARY` as the default privileges of `PUBLIC` on databases. Revoke `CONNECT` from `PUBLIC` on the databases the agent has no business in, and grant it back to the roles that need it.

You use a superuser role for the agent. A superuser skips the grants, so nothing above applies.

You never restore the backup. Then the first restore happens on the day the data is gone.

## In Valakes

Mark the host as Production in Valakes. Then a command that uses a saved credential always waits for your approval, even when the agent has Full access, and a `TRUNCATE` or `DROP DATABASE` given to `psql` stops on a risk card that the agent cannot override. The Help says the command check reads the command line and not SQL that comes from a file, so the limits of the database role still have to be in place. See [agents](https://valakes.com/features/ai-agents/) for the levels, and [the use case](https://valakes.com/use-cases/ai-agents-on-servers/) for an agent's path from its token to the audit log.

## In Valakes

On a host marked Production, a command that uses a saved credential always waits for your approval, even when the agent has Full access, and a TRUNCATE or DROP DATABASE typed into psql stops on a risk card the agent cannot override. [Feature: AI agents](https://valakes.com/features/ai-agents/)

[Read the docs: Agents](https://docs.valakes.com/agents/)

## Questions

### Can I take away the right to DROP from the role that owns a table?

No. The GRANT manual says the right to modify or destroy an object belongs to its owner and cannot be granted or revoked in itself. Make another role the owner, give the agent no membership in it, and the agent cannot drop the table.

### Is default_transaction_read_only enough on its own?

No. It is a default for new transactions, and a role can change it for its own session, as the commands above show. Use it together with the grants, never in place of them.

### Why give the agent a copy and not the real database?

On a copy a wrong command costs nothing. The agent can drop and rebuild tables there, and the production tables stay as they are. Leave out the tables the agent has no use for, as the dump above leaves out api_tokens.

## References

- [PostgreSQL 16 manual, GRANT](https://www.postgresql.org/docs/16/sql-grant.html)
- [PostgreSQL 16 manual, REVOKE](https://www.postgresql.org/docs/16/sql-revoke.html)
- [PostgreSQL 16 manual, CREATE ROLE](https://www.postgresql.org/docs/16/sql-createrole.html)
- [PostgreSQL 16 manual, ALTER ROLE and the IN DATABASE clause](https://www.postgresql.org/docs/16/sql-alterrole.html)
- [PostgreSQL 16 manual, default_transaction_read_only](https://www.postgresql.org/docs/16/runtime-config-client.html)
- [PostgreSQL 16 manual, SET TRANSACTION and what a read-only transaction forbids](https://www.postgresql.org/docs/16/sql-set-transaction.html)
- [PostgreSQL 16 manual, privileges](https://www.postgresql.org/docs/16/ddl-priv.html)
- [PostgreSQL 16 manual, pg_dump](https://www.postgresql.org/docs/16/app-pgdump.html)

## Related articles

- [How do I give an AI agent SSH access with the least damage?](https://valakes.com/learn/ai-agents-on-servers/): Give the agent its own account on the server, with no sudo, no password in the prompt and nothing secret in its reach. Let it read first, allow root for one exact command at most, make every change wait for a person, and keep a record of what ran.
- [How do I keep a record of what an AI agent ran on a server?](https://valakes.com/learn/agent-audit-trail/): Do not count on shell history. Give the agent its own account and key, log sshd at VERBOSE so each login shows the key fingerprint, and let sudo record the input and output of anything the agent runs as root.
- [How do I run psql without typing the password?](https://valakes.com/learn/psql-without-password-prompt/): Put one line in ~/.pgpass in the form hostname:port:database:username:password and run chmod 600 on the file. psql then finds the password itself, and psql -w makes a script fail with an error instead of waiting for a prompt that nobody can answer.
- [How do I change permissions recursively on Linux?](https://valakes.com/learn/file-permissions/): Do not use one number for the whole tree. Run find site -type d -exec chmod 755 {} + for folders and find site -type f -exec chmod 644 {} + for files, then chown -R to set the owner and group. chmod -R 777 lets every account on the machine change your files.
