Give the agent its own copy of the data to write to, look at exactly which rows it changed, and let a rule decide what reaches the real database. The agent never holds a credential for your Postgres, and nothing it does is live until it merges. This post walks through that setup with Chronos DB beside a Postgres you already run. It takes about ten minutes.
Why is giving an agent write access risky?
Because a database role can say which tables an agent may write, not which changes are sensible. An UPDATE without the right WHERE is allowed. So is deleting every row a filter happened to match. The usual answers each give something up:
| Approach | What you give up |
|---|---|
| Read-only agents | The agent can find the problem and can’t fix it. |
| A person approves every statement | It stops scaling at the second agent, and people approve SQL they haven’t read. |
| A staging copy | The work has to be done again on production, by hand, against data that has moved on. |
| Write access and backups | Undoing one bad run means restoring, and losing everyone else’s work since. |
What is missing in each is the same thing: a place for the agent to work that is real data but not live, and a checked way to bring its work back.
What does a safe setup look like?
Four parts, and the order matters:
- 01A branch per runEach run gets a world: a copy-on-write copy of every table, made in about 2 microseconds whatever the size. Nobody else sees its writes.
- 02A diffWhen the run ends you have every row it changed, with the value before and after. Not a log of statements: the rows.
- 03Rules that decideA merge policy says what may land on its own, such as at most 500 rows and no deletes. The rest waits for a person.
- 04A checked write-backThe merge is one transaction in your Postgres. Every row is locked and compared with what the agent saw; if one changed, nothing is written.
How do I set it up beside my Postgres?
Install Chronos DB, then attach it to the tables the agent will work on. Attach copies those tables into a local folder; your application keeps using Postgres exactly as before.
curl -fsSL https://github.com/2090-labs/Chronos-DB/releases/latest/download/install.sh | shexport CHRONOS_SOURCE_URL='postgres://agent_writer:...@staging-db:5432/crm?sslmode=verify-full'chronos attach ./crm --tables public.leads,public.accountsexport CHRONOS_TOKEN=$(openssl rand -hex 24) # the guest's: reads, forks, its own worldsexport CHRONOS_ADMIN_TOKEN=$(openssl rand -hex 24) # yours: the database's own userchronos serve ./crm # Postgres protocol on 127.0.0.1:5433The connection URL is read from the environment, never from the command line, and is not written into the folder. The role needs to be allowed to write those tables. Attach makes one table of its own in your database, chronos.merges, to record each merge it writes.
Start the server with both tokens. Without them, anyone who connects to the port from the same machine is the database’s own user, and an agent that simply leaves its token out could approve its own merges. With them, a connection is who its password says: the admin token is you, an agent’s token is that agent, the server token is a guest that can change only the worlds guests forked, and a connection with no password is refused.
How do I decide what merges without me?
Write the rules once, as a merge policy, and give the agent an account that is held to it. Start strict: no deletes land on their own.
-- psql "postgres://you:$CHRONOS_ADMIN_TOKEN@127.0.0.1:5433/main", as youcreate merge policy agents with (max_deletes = 0, max_rows = 500, review_tables = 'payments');create agent bot with (can = 'read,fork,write_own,write_main,merge_own', policy = 'agents');-- the agent's token is in the token column, shown only nowA merge that changes at most 500 rows, deletes none and stays out of payments goes through by itself. Any other is refused with its reasons and queued for you. The agent connects with its own token, not your database password.
What does one agent run look like?
The agent opens a world, works in ordinary SQL, and asks to merge:
-- psql "postgres://bot:<token>@127.0.0.1:5433/main", as the agentcreate world run_42 with (task = 'clean up stale leads');switch world run_42;update leads set status = 'stale' where last_contact < now() - interval '1 year';delete from leads where email is null;diff world run_42; -- table, id, change, before, aftermerge world run_42 dry run; -- what the merge would do, row by row, and why it would be heldmerge world run_42; -- it deletes rows, so the policy holds itPostgres has not changed. The run’s deletes broke the rule, so the merge waits in the review queue. You look, then decide:
show reviews; -- run_42, why it is held, and its version: say 7diff world run_42; -- read what you are about to approvemerge world run_42 at version 7; -- approve exactly that: one checked transaction in Postgres-- or: drop world run_42; -- reject: Postgres never saw itApprove with the version you read. A world’s version moves on with every write, so if the agent wrote more after you looked, at version 7 merges nothing and says so. A bare merge world run_42 would land whatever is in the world by then, read or not.
For Claude Code or any MCP client the setup is one line, and by default the agent can fork, write its own worlds and preview a merge, while a person merges:
claude mcp add chronos -- chronos mcp ./crmWhat if the data changed while the agent worked?
Then the merge does not overwrite it. At merge time Chronos locks each row the world changed in Postgres and compares it with what Chronos last saw. If any of them changed in Postgres in the meantime, nothing is written and the merge returns those rows as conflicts for you to settle. Values your Postgres triggers or defaults set come back into Chronos after a merge.
What if a bad change lands anyway?
Put that one change back, without a restore. Two ways:
undo merge of world run_42; -- one mergeundo agent bot since '-2 hours'; -- everything one agent did since thenBoth put the rows back as they were, row by row, and leave everyone else’s changes alone. A merge written to your Postgres can also be undone by its id for 30 days, and the undo goes through Postgres the same checked way the merge did.
What doesn’t this protect against?
- An agent that runs as you. The
chronoscommands on the database’s own machine join the server as the operating-system user who owns the folder, and tokens do not apply there. Run agents as another user or on another machine, and hand them only their token. - An agent that calls other systems directly. A world holds database changes; an email the agent sent is sent.
- Rules you didn’t write. A policy that allows 500 rows allows 500 wrong rows. Add merge checks for the things that must stay true, such as “every lead list keeps at least one lead”.
- SQL that Chronos doesn’t run. It is not Postgres: it runs much of Postgres 16’s SQL, not all of it. Postgres compatibility says how far it is, as measured.
More on each part: Beside Postgres, Merge policies and reviews, Time travel and undo. If your own product’s agents change your customers’ data, one customer at a time, that is what Chronos Gateway is for.
Questions
Can an AI agent write to a production Postgres database safely?
Yes, if it never writes to it directly. Give each agent run its own branch of the data, review the rows it changed, and let a merge policy decide which changes reach Postgres. With Chronos DB the merge is one checked transaction, and anything outside your rules waits for a person.
Does the agent need my database password?
No. The agent connects to Chronos with its own token and works in a world, a private copy of the tables. Only Chronos holds the Postgres connection, and it writes there only when a merge is approved. Start the server with a server token and an admin token, so that a connection without the agent’s token is a guest and not the database’s own user.
What happens if the data changed while the agent was working?
The merge is refused rather than overwriting it. Each row the agent changed is locked in Postgres and compared with what it was when Chronos last saw it; if any changed, nothing is written and those rows come back as conflicts.
Can I undo what an AI agent changed?
Yes. Undo one merge, or everything one agent did since a time, row by row, without restoring a backup and without touching other changes. A merge written to your Postgres can be undone by its id for 30 days.
