pgbrake

io.github.lexang由 GitHub 账号 lexang 发布v0.3.0更新于 Nov 7, 2026

Postgres brake for AI agents: risky SQL waits for approval, UPDATE/DELETE get one-command undo.

概览

AI 生成的概览

本地 MCP 服务器,在 AI 代理与 Postgres 之间加一道闸门:高风险 SQL 需人工批准,写入可用一条命令撤销。

可以做什么
  • 让代理对 Postgres 执行 SQL,语句由真正的 Postgres 解析器解析
  • 将高风险写入挂起等待人工批准:DDL、无 WHERE 的 UPDATE/DELETE、超过行数上限的写入、upsert 与 MERGE
  • 直接拒绝 ALTER SYSTEM、角色或数据库的创建与删除、COPY PROGRAM、DO 块等语句
  • 按变更 ID 撤销单条 UPDATE 或 DELETE,若数据已被改动则拒绝执行
  • 在命令行查看待批准项、可撤销变更和完整审计日志
适用场景
当你允许 AI 编程代理写入真实的 Postgres 数据库并希望有安全网时使用:破坏性语句需批准、写入可回退、留有审计记录,且规则在服务器内部强制执行,而非依赖客户端提示。
运行要求
本地 Python 环境(pip 或 uvx),以 stdio 进程运行。需要 PGBRAKE_DSN 中的 Postgres 连接字符串(可回退到 DATABASE_URL)。可选设置控制行数上限、是否允许 DDL、只读模式、语句超时和日志位置。人工批准在客户端提示或交互式终端中完成。
安装前请注意
需要具备写权限的数据库连接字符串,请谨慎限定该角色的权限。写入、DDL 和删除会作用于真实数据库;撤销仅覆盖经 pgbrake 修改的行,不是备份,也无法回退 TRUNCATE、DDL、upsert 或 MERGE。免费版只保留最近 20 条可撤销变更。另有付费 Pro 版本,许可证密钥离线校验。

安装

在 SourceWeft 中

  1. 打开 控制台中的 pgbrake,将其添加到工作区。
  2. 为需要使用其工具的对话启用该服务。

Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。

其他 MCP 客户端

参照 仓库 中的启动说明。

README

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.

[pgbrake demo]

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 = 42pgbrake> 1 row affected; undo with change id chg_8e01b7d2
you$ pgbrake undo chg_8e01b7d2undid 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/DELETE without WHERE, any write over the row limit (default 100), upserts and MERGE.
  • Never allowed: ALTER SYSTEM, role/database creation and drops, COPY ... PROGRAM, DO blocks.
  • 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

bash
pip install pgbrake      # or: uvx pgbrake serve

Claude Code:

bash
claude mcp add pgbrake --env PGBRAKE_DSN="postgresql://me@localhost/app" -- uvx pgbrake serve

Codex (~/.codex/config.toml):

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 youpgbrake approve <id>       # approve (interactive terminal only)pgbrake deny <id>pgbrake changes            # undoable changespgbrake undo <change-id>   # reverse onepgbrake log                # audit log

Settings (env)

VariableDefault
PGBRAKE_DSN—connection string (falls back to DATABASE_URL)
PGBRAKE_MAX_ROWS100writes touching more rows need approval
PGBRAKE_ALLOW_DDL0set 1 to let schema changes through (still logged)
PGBRAKE_READ_ONLY0set 1 to refuse every write
PGBRAKE_STATEMENT_TIMEOUT_MS15000
PGBRAKE_JOURNAL~/.pgbrake/journal.db

Limits

  • Undo covers rows changed through pgbrake. It is not a backup: keep real backups.
  • TRUNCATE, DDL, upserts and MERGE are 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

$29 one-time for the first 100 licenses (then $39). No subscription. 14-day refund. Buy pgbrake Pro

  • Migration guard for agent terminal commands. A Claude Code PreToolUse hook catches prisma migrate reset --force, drizzle-kit push --force, alembic downgrade base and friends, takes a pg_dump snapshot first (when PGBRAKE_DSN is set), checks pg_restore can read it, then asks you. If the snapshot fails, the command is blocked instead (override with PGBRAKE_ALLOW_NO_SNAPSHOT=1). Set PGBRAKE_VERIFY_RESTORE=1 to also restore the snapshot into a throwaway database and compare table counts before you're asked; if that restore fails, the command is blocked. The drill needs CREATEDB rights (or PGBRAKE_DRILL_DSN pointing at a server where it has them) and takes about as long as a restore. Install with pgbrake install-hook.
  • Unlimited undo history (free keeps the last 20).
  • Audit export: pgbrake log --export csv|json.

After paying you land on a page that shows your key. Activate it once:

pgbrake license <your-key>     # or set PGBRAKE_LICENSE

Keys are verified offline; pgbrake never phones home.

来源:README.md,提交 da29fa1

工具

0
SourceWeft 尚未在沙箱中启动过该软件包,因此其工具未知。

版本历史

4
  1. v0.3.0最新Nov 7, 2026
  2. v0.2.1Nov 5, 2026
  3. v0.2.0Nov 1, 2026
  4. v0.1.1Oct 29, 2026

评分

已满足适用检查项的 50%

该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。

  • 近 30 天内可连通25 分

    请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。

  • 已声明认证方式10 分

    请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。

  • 已列出工具15 分

    请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。

  • 描述必需15 分
  • 图标10 分
  • 文档15 分
  • 许可证10 分