pgbrake
An undo button for your database when an AI agent is driving.
pgbrake is a local MCP server that sits between Claude Code / Codex / Cursor and your Postgres. Every write the agent makes gets a change id you can reverse with one command. Risky SQL waits for a human. Everything is logged. Nothing leaves your machine.
agent> DELETE FROM users WHERE created_at < '2025-01-01'
pgbrake> held: DELETE would touch 4,812 rows (limit 100) — approval apv_3f9c21aa
agent> DELETE FROM users WHERE id = 42
pgbrake> 1 row affected; undo with change id chg_8e01b7d2
you$ pgbrake undo chg_8e01b7d2
undid delete on "users": 1 row(s) restored
Why
Claude Code checkpoints rewind files, not databases. Client-side permission prompts and hooks can be skipped or misfire for MCP calls, and a tool's own "destructive" label is just a hint. pgbrake enforces its rules inside the server, so they hold no matter which agent or client calls it.
What it does (free, MIT)
- Real SQL parsing (the Postgres parser via pglast), not regex.
- Held for approval: DDL (
DROP,ALTER,TRUNCATE...),UPDATE/DELETEwithoutWHERE, any write over the row limit (default 100), upserts andMERGE. - Never allowed:
ALTER SYSTEM, role/database creation and drops,COPY ... PROGRAM,DOblocks. - One-command undo: before each
UPDATE/DELETE, the affected rows are saved locally; inserts are tracked by primary key. Undo refuses if the rows changed since, unless you--force. - Approval only by a human: in the client's own prompt (MCP elicitation) or
pgbrake approve <id>in an interactive terminal. Approvals are one-shot, expire in 15 minutes, and are bound to the exact SQL. - Audit log of every statement, decision and row count, in
~/.pgbrake/journal.db. - Multi-statement calls run in one transaction: all or nothing.
Install
pip install pgbrake # or: uvx pgbrake serve
# before the PyPI release: pip install git+https://github.com/lexang/pgbrake
Claude Code:
claude mcp add pgbrake --env PGBRAKE_DSN="postgresql://me@localhost/app" -- uvx pgbrake serve
Codex (~/.codex/config.toml):
[mcp_servers.pgbrake]
command = "uvx"
args = ["pgbrake", "serve"]
env = { PGBRAKE_DSN = "postgresql://me@localhost/app" }
Then remove any other database MCP / psql access from the agent, so pgbrake is the only way in.
In Claude Code, add "Bash(psql:*)" and "Bash(pgbrake approve:*)" to permissions.deny.
Commands
pgbrake pending # SQL waiting for you
pgbrake approve <id> # approve (interactive terminal only)
pgbrake deny <id>
pgbrake changes # undoable changes
pgbrake undo <change-id> # reverse one
pgbrake log # audit log
Settings (env)
| Variable | Default | |
|---|---|---|
PGBRAKE_DSN |
— | connection string (falls back to DATABASE_URL) |
PGBRAKE_MAX_ROWS |
100 | writes touching more rows need approval |
PGBRAKE_ALLOW_DDL |
0 | set 1 to let schema changes through (still logged) |
PGBRAKE_READ_ONLY |
0 | set 1 to refuse every write |
PGBRAKE_STATEMENT_TIMEOUT_MS |
15000 | |
PGBRAKE_JOURNAL |
~/.pgbrake/journal.db |
Limits
- Undo covers rows changed through pgbrake. It is not a backup: keep real backups.
TRUNCATE, DDL, upserts andMERGEare held for approval but cannot be undone.- Updating a table with no primary key needs approval and cannot be undone.
- The free version keeps the last 20 undoable changes.
Pro (coming)
Unlimited undo history, a migration guard for agent terminal commands
(prisma migrate reset --force, drizzle-kit push --force, alembic downgrade base...: snapshot first,
then ask), per-project policy files, and audit export. One-time license.
Metadata
Release files for pgbrake 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pgbrake-0.1.0.tar.gz | 13.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pgbrake-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.7 kB
Release files / pgbrake-0.1.0.tar.gz
| Download URL | pgbrake-0.1.0.tar.gz |
|---|---|
| Size | 13.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9699de81bcc5f054ec40c496940e46e0bc20c55dc2709ab7b471c72a585811de
|
|
BLAKE2b-256 checksum How to use checksums |
92984f3ba0524b70c4cb447386ea0347ad5c6a7f5203e0134586b448c4054223
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Release files / pgbrake-0.1.0-py3-none-any.whl
| Download URL | pgbrake-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4de1ce9cdeef19da419dff934c7f38b66ddec40a1a894255ad535812e15d40c
|
|
BLAKE2b-256 checksum How to use checksums |
28f43200cf8a75e46e38d5ac20fb2131a7a29e9b1aea2631263b1d2e765a1e98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Security for pgbrake 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.