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

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.

Last updated

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

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, 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.

$ 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:

$ 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:

$ 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:

$ 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:

$ 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:

$ 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:

$ 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 covers the account side, with the permissions article 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:

$ 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:

$ 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 for the levels, and the use case for an agent’s path from its token to the audit log.

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

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

The app is free.

Get notified at launch

macOS is coming to the Mac App Store. iOS and Android later.