
pgops
io.github.arzharch由 GitHub 账号 arzharch 发布v0.1.9更新于 Oct 24, 2026
Safe, audited PostgreSQL operations for AI agents: queries, migrations, EXPLAIN, containers
概览
一个本地 MCP 服务器,让 AI 助手在受保护、可审计的前提下检查、查询、诊断和迁移 PostgreSQL 数据库。
- 可以做什么
- 检查数据库架构和表结构,并读取架构摘要、健康状态和近期审计记录等资源。执行只读查询、受保护的写入以及 EXPLAIN 执行计划,并给出解析后的结论和调优建议。规划、说明、应用、回滚和查看迁移,附带锁影响与风险分析。通过索引建议和数据库健康检查诊断性能。查看容器环境:拓扑、关联、日志和统计,并在启用审批模式时使用受限的重启和 exec 工具。
- 适用场景
- 适合让助手回答关于 Postgres 数据库的问题、诊断慢查询,或在人工确认和审计日志的保护下规划和执行架构迁移。面向单个工程师和少量数据库,而非多租户场景。
- 运行要求
- 通过 uvx(Python)或已发布的容器镜像(Docker)作为本地进程运行。需要 PGOPS_DSN 中的 PostgreSQL 连接字符串,可选 PGOPS_READONLY_DSN 中的只读角色连接。可选设置包括 PGOPS_READ_ONLY、PGOPS_APPROVAL_MODE、PGOPS_AUDIT_LOG 以及行数和超时变量。需要能访问数据库的网络。
安装
在 SourceWeft 中
- 打开 控制台中的 pgops,将其添加到工作区。
- 为需要使用其工具的对话启用该服务。
Desktop only,通过 STDIO。 STDIO 服务会启动本地进程,因此需要 SourceWeft 桌面宿主。
其他 MCP 客户端
参照 仓库 中的启动说明。
README
pgops-mcp
Let an AI assistant operate your PostgreSQL database — without letting it wreck anything.
Point Claude, Cursor, or VS Code at a database and ask questions in plain English. Your assistant gets 18 tools for inspecting schemas, running queries, diagnosing slow ones, and planning migrations. What it does not get is a way to quietly destroy your data.
What it actually looks like
Every number below is real output from the demo database in this repo — 1.2M rows, 114 MB.
You: I want to index
orders.status
Assistant: (calls
migration.plan)
Assistant: A plain index build blocks writes for roughly 4 seconds.
CONCURRENTLYis slower overall but keeps writes working. Which do you want?
The tool didn't return a raw plan for the model to interpret — it returned a verdict, a duration estimate with an honest confidence level, and the safer alternative.
query.explain works the same way. Instead of handing back JSON, it says things like:
And when something is genuinely dangerous
You: clear out the orders table
Assistant: (calls
query.writewithDELETE FROM orders)
Where your client supports it, the approval prompt goes to you — not to the assistant. Nothing runs until a human answers, and the refusal is written to the audit log whether or not you approve.
That last part is the point. The assistant cannot approve its own dangerous action, because it is not the one being asked. Where a client can't show a prompt, it degrades to a single-use token bound to that exact statement — never to "allowed".
Why this exists
Most Postgres MCP servers are thin query wrappers: introspect and SELECT. None handle
migrations with lock-impact analysis, none diagnose performance from EXPLAIN and
pg_stat_statements, and none understand the container the database runs in. Agents
operating databases today are doing it blind, and without guardrails.
pgops-mcp is the operations brain: schema intelligence → guarded queries → migration
engine → performance diagnosis → environment awareness, with a safety architecture that
makes every action classifiable, confirmable, and auditable.
Native AI/ML Extension Support: Because pgops builds on core Postgres catalogs rather than brittle regex parsing, it inherits native support for custom types and extensions like pgvector. Tools like migration.plan and query.explain understand vector types (vector(384)) and hnsw indexes out of the box, with zero configuration.
New here? docs/GETTING_STARTED.md is a 15-minute guided tour that assumes no MCP knowledge.
Tool surface
* Not registered at all unless the server runs with --approval-mode, and even then
each call needs a confirmation token. container.exec additionally enforces a read-only
diagnostic command allowlist — it does not offer a shell. The Docker socket is
root-equivalent on the host, so the default is read-only access.
Safety model (the core differentiator)
- Separate read-only / read-write connection roles; tools bind to the right role
- Statement classification before execution — unbounded
DELETE/UPDATEblocked - Destructive actions require explicit confirmation tokens
- Every executed statement lands in an append-only audit log with timing and verdict
- Runaway-query cancellation with timeout tiers
MCP surface
Remote access & agent tokens
stdio needs no auth — the server is a subprocess your client spawns, with no open port. HTTP does, so it refuses to start without a key:
The server holds only the public key, so it can verify tokens but never mint them.
Scopes (pgops:read / pgops:write / pgops:admin) map to the same danger tiers as the
guardrails, and a tool with no scope entry requires admin — deny by default. Binds
loopback unless you say otherwise.
Install
pgops-mcp is an MCP server, not a Python library — nothing in it is meant to be
imported, and pgops.* carries no API-stability promise. You install it the way you
install any MCP server: point your client at it.
Claude Desktop / Cursor / VS Code:
uvx fetches and runs it in a throwaway environment — nothing to install first, and
nothing added to your own project's dependencies.
Or run the container, if you would rather not put a Python toolchain on the machine that talks to your database:
Two things the container changes: mount a volume at /var/lib/pgops or the audit log
dies with the container, and localhost inside a container is the container itself —
use host.docker.internal or a compose service name.
Check the connection before wiring a client to it:
Both paths install the same server and are listed together in the
MCP Registry entry — they fail for different
people. uvx needs nothing preinstalled but assumes the host may run Python; the
container assumes only Docker.
See SETUP.md for configuration, HTTP transport, agent tokens and troubleshooting, and CONTRIBUTING.md to run it from a source checkout.
Docs
Links are absolute so they resolve from the PyPI project page as well as from GitHub.
Using it
How it works
Contributing
How it's verified
471 tests, and the ones that matter run against a real PostgreSQL 16 in a
container — not mocks. That is a deliberate decision (ADR-005):
a guardrail proven only against a fake has been proven against the wrong thing. The
interesting failures — default_transaction_read_only, lock escalation, transactional
DDL, relfilenode changes on rewrite — are behaviours of the real database.
The red-team suite has found real bugs, which is the argument for having it: it caught a
confirmation token issued for a refused statement being redeemable against a different
one, and a pgops:read token that could call query.write because the scope table was
documentation rather than enforcement.
Known limits
Stated here rather than left to be discovered:
- No per-session database isolation. Auth identifies the caller and scopes limit what they may do, but every caller shares one connection manager and one audit log. Built for one engineer and a few databases, not multi-tenant SaaS.
index.advisenames the table taking sequential scans, not the column to index — that needs per-statement plan inspection. It says so instead of inventing aCREATE INDEX.DROP INDEX/DROP CONSTRAINTcannot be rolled back, because the object's definition is not captured before the drop. The rollback refuses and explains why rather than reconstructing a guess.
Sample of what migration.plan returns for a type change on the 1.2M-row orders:
Try it without a database of your own
A seeded stack with the 1.2M-row orders table used in every example above. Host port
5435, so it does not collide with a local Postgres on 5432:
MIT licensed. Contributions welcome — see CONTRIBUTING.md.
来源:README.md,提交 aaadd7e
工具
0版本历史
1- v0.1.9最新Sep 16, 2026
评分
已满足适用检查项的 50%
该评分衡量此收录条目的完整程度,依据注册中心条目、端点与公开来源计算得出。它不是对服务器本身的评价,也不表示服务器安全或当前可用:可连通性是带日期的检查结果,评分也不包含安装量或用户评价。
- 近 30 天内可连通25 分
请保持端点能够响应 MCP 握手;SourceWeft 每 30 天探测一次。
- 已声明认证方式10 分
请在注册中心条目中声明凭据请求头,或保持端点能够响应探测,以便判定其认证方式。
- 已列出工具15 分
请让服务器在无凭据时响应 tools/list,或发布可在沙箱中无凭据启动的软件包。
- 描述必需15 分
- 图标10 分
- 文档15 分
- 许可证10 分