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 分