Agent Skill

Use this skill when an agent needs PostgreSQL access that is structured, read-only by default, safe for scripts, or reachable only across SSH/container boundaries. Prefer afpsql over parsing human psql tables, SSHing in to run psql, or docker exec/kubectl exec with human output.

For flag-level detail, run afpsql --help or afpsql --help --recursive --output markdown. This skill covers behavior, decisions, and recovery only.

Core Rules

Discovering Schema

Prefer afpsql inspect over hand-writing information_schema / pg_catalog queries:

For query plans, wrap with --explain (EXPLAIN (FORMAT JSON)) or --explain-analyze (also runs the statement; writes still need write permission). The plan JSON arrives in a normal code:"result" row.

Validating Before Executing

afpsql --dry-run --sql '...' --param 1=... [--param 2=...] opens a connection, runs PREPARE inside a transaction that is rolled back, and emits a code:"dry_run" event with the inferred param_types, output columns, and any prepare error. Use this to catch placeholder mismatches, missing tables, and type confusion before letting a query actually run.

Branching on Failures

Results that Don’t Fit Inline

If a code:"result" event carries truncated:true, the underlying statement still ran in full, but the returned rows is only a prefix (see truncated_at_rows / truncated_at_bytes). For UPDATE ... RETURNING this means the writes happened; only the RETURNING projection was capped. Either narrow the query (WHERE / LIMIT) or rerun with --stream-rows to receive the full set in batches.

Multi-Statement Atomicity (Pipe Mode)

Each query is its own transaction by default. For atomic multi-statement work, open an explicit transaction:

{"code":"begin","id":"b","permission":"write"}
{"code":"query","id":"q1","sql":"insert into orders ...","options":{"permission":"write"}}
{"code":"query","id":"q2","sql":"update inventory ...","options":{"permission":"write"}}
{"code":"commit","id":"c"}

Non-Obvious Behaviors

Setup Checklist

Only run setup when asked to prepare or repair the machine; do not run it before every query.

afpsql --version || brew install agentfirstkit/tap/afpsql
cargo install agent-first-psql  # fallback when Homebrew is unavailable
afpsql skill install            # personal Claude/Codex skill
afpsql psql install             # optional: psql-compatible wrapper

Troubleshooting