postgres-cli
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePostgres CLI (V2)
Postgres CLI(V2版)
Use to query PostgreSQL through named connection targets.
postgres-cli使用通过已命名的连接目标查询PostgreSQL数据库。
postgres-cliPlatform Support
平台支持
- is a launcher script.
scripts/postgres-cli - Prebuilt binaries are expected in for:
scripts/bin/- macOS arm64 ()
postgres-cli-darwin-arm64 - Linux x86_64 ()
postgres-cli-linux-x86_64 - Windows x86_64 ()
postgres-cli-windows-x86_64.exe
- macOS arm64 (
- If no compatible binary exists and source + Cargo are available, launcher falls back to .
cargo run --release
- 是一个启动脚本。
scripts/postgres-cli - 预编译二进制文件需放置在目录下,支持以下平台:
scripts/bin/- macOS arm64()
postgres-cli-darwin-arm64 - Linux x86_64()
postgres-cli-linux-x86_64 - Windows x86_64()
postgres-cli-windows-x86_64.exe
- macOS arm64(
- 若不存在兼容的二进制文件,且源码和Cargo可用,启动脚本会回退到方式运行。
cargo run --release
Available Scripts
可用脚本
- - Launcher that selects a platform binary.
scripts/postgres-cli - - Builds and places a binary for current host into
scripts/build-release-binary.sh.scripts/bin/ - - Downloads release binaries into
scripts/refresh-binaries-from-release.sh <tag>.scripts/bin/
- - 用于选择对应平台二进制文件的启动器。
scripts/postgres-cli - - 为当前主机构建二进制文件并放置到
scripts/build-release-binary.sh目录。scripts/bin/ - - 从发布版本下载二进制文件到
scripts/refresh-binaries-from-release.sh <tag>目录。scripts/bin/
When To Use
使用场景
- The user asks to inspect Postgres data.
- The user asks to run SQL against a configured target.
- The user asks for schema introspection.
- The user asks to validate DB CLI config or debug connectivity.
- The user asks to refresh schema cache for agent context.
- 用户需要检查Postgres数据时。
- 用户需要针对已配置的目标运行SQL时。
- 用户需要进行模式自省操作时。
- 用户需要验证数据库CLI配置或调试连接问题时。
- 用户需要刷新Agent上下文的模式缓存时。
Setup
安装配置
Read Setup Guide.
请阅读安装指南。
V2 Command Contract
V2 命令约定
Global flags (before subcommand):
- (optional; default cwd)
--project-root <path> - (optional if
--target <name>set)default_target - (default
--format <json|text|csv|tsv>)json - (optional output file)
--output <path> - (suppresses text summary)
--no-summary
Subcommands:
query- input: exactly one of ,
--sql,--sql-file--stdin - flags: (default
--mode <read|write>),read--timeout-ms
- input: exactly one of
explain- input: exactly one of ,
--sql,--sql-file--stdin - flags: ,
--analyze,--verbose,--buffers,--settings,--wal--timeout-ms
- input: exactly one of
introspect- required:
--kind <schemas|tables|columns|indexes|constraints|views|materialized-views|functions|triggers|enums|rowcounts|rowcounts-exact> - optional filters: (repeatable),
--schema(repeatable)--table schema.table
- required:
schema-cache update- flags: ,
--all-tables,--with-markdown,--table-file-naming <table|schema-table>--timeout-ms
- flags:
targets listconfig validatedoctor
全局标志(子命令之前):
- (可选;默认当前工作目录)
--project-root <path> - (若已设置
--target <name>则可选)default_target - (默认
--format <json|text|csv|tsv>)json - (可选输出文件路径)
--output <path> - (禁用文本摘要)
--no-summary
子命令:
query- 输入:必须指定、
--sql、--sql-file中的一个--stdin - 标志:(默认
--mode <read|write>)、read--timeout-ms
- 输入:必须指定
explain- 输入:必须指定、
--sql、--sql-file中的一个--stdin - 标志:、
--analyze、--verbose、--buffers、--settings、--wal--timeout-ms
- 输入:必须指定
introspect- 必填:
--kind <schemas|tables|columns|indexes|constraints|views|materialized-views|functions|triggers|enums|rowcounts|rowcounts-exact> - 可选过滤器:(可重复使用)、
--schema(可重复使用)--table schema.table
- 必填:
schema-cache update- 标志:、
--all-tables、--with-markdown、--table-file-naming <table|schema-table>--timeout-ms
- 标志:
targets listconfig validatedoctor
Safety Rules
安全规则
- Prefer read targets for normal data inspection.
- blocks mutating SQL.
query --mode read - Mutating SQL requires and
query --mode writeon target.allow_write=true - on mutating SQL requires write-enabled target.
explain --analyze
- 正常数据检查时优先使用只读目标。
- 会阻止修改数据的SQL。
query --mode read - 修改数据的SQL需要使用且目标需设置
query --mode write。allow_write=true - 对修改数据的SQL执行需要目标启用写入权限。
explain --analyze
Command Patterns
命令示例
Run read query:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read query --sql "SELECT now();"Run write query intentionally:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-write query --mode write --sql "UPDATE users SET active = true WHERE id = 1;"Introspect tables:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read introspect --kind tablesExplain a query:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read explain --sql "SELECT * FROM users WHERE id = 1;"Validate config:
bash
scripts/postgres-cli --project-root /path/to/repo config validateDoctor connectivity:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read doctorUpdate schema cache (JSON only):
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read schema-cache updateUpdate schema cache with markdown:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read schema-cache update --with-markdown运行只读查询:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read query --sql "SELECT now();"有意运行写入查询:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-write query --mode write --sql "UPDATE users SET active = true WHERE id = 1;"自省表结构:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read introspect --kind tables解释查询语句:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read explain --sql "SELECT * FROM users WHERE id = 1;"验证配置:
bash
scripts/postgres-cli --project-root /path/to/repo config validate诊断连接问题:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read doctor更新模式缓存(仅JSON格式):
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read schema-cache update更新带Markdown格式的模式缓存:
bash
scripts/postgres-cli --project-root /path/to/repo --target webshop-read schema-cache update --with-markdownProgressive Schema Loading
渐进式模式加载
When schema context is needed, use this order:
- Read .
.agent/postgres-cli/schema/index.json - Load only required files from .
.agent/postgres-cli/schema/tables/*.json - Read when join/relationship reasoning is needed.
.agent/postgres-cli/schema/relations.json - If markdown was generated, consult files only for human-friendly display.
.md - If cache is missing or stale, run .
schema-cache update
当需要模式上下文时,请按照以下顺序操作:
- 读取文件。
.agent/postgres-cli/schema/index.json - 仅从加载所需的文件。
.agent/postgres-cli/schema/tables/*.json - 当需要关联/关系推理时,读取文件。
.agent/postgres-cli/schema/relations.json - 若已生成Markdown文件,仅在需要友好的人类可读展示时参考文件。
.md - 若缓存缺失或过期,运行命令。
schema-cache update
Agent Guidelines
Agent使用指南
- Always pass unless
--targetis known and stable.default_target - Default to for machine parsing.
--format json - Prefer unless the user explicitly requests data mutation.
query --mode read - Return relevant result rows and summarize key findings.
- 除非已知是稳定的,否则始终传递
default_target参数。--target - 默认使用以方便机器解析。
--format json - 除非用户明确要求修改数据,否则优先使用。
query --mode read - 返回相关的结果行并总结关键发现。