Directory Compare FCRS-1.0 /llms.txt
← Back to Directory

PostgreSQL psql

$ psql Official High Risk
PostgreSQL Global Development Group · Database & Data · PostgreSQL License · macos / linux / windows

psql is the official PostgreSQL command-line client. While famous as an interactive shell, it supports crisp non-interactive agent execution via -c, -X (skip .psqlrc), -v ON_ERROR_STOP=1, --csv, and PostgreSQL's json_agg / row_to_json output.

FCRS-1.0 Score
83 /100
Good
Medium Confidence
Zero-Friction Install
brew install libpq
CLI vs MCP Decision CLI Preferred vs MCP

psql -X -v ON_ERROR_STOP=1 --csv -c "..." is ubiquitous, fast, and scriptable; use PGOPTIONS='-c default_transaction_read_only=on' for guaranteed read-only sessions.

Agent-Readiness Checklist

— JSON Output
— Field Filtering
✓ Non-Interactive
— Dry-Run
— Read-Only Mode
— NO_COLOR Clean
✓ Self-Describing
— Official SKILL.md

Static evaluation (not yet probed on this host). Run npm run data:probe locally to capture live verification.

FCRS-1.0 6-Dimension Breakdown

Agent-Readiness 19 / 25

Supports -X, -c, -t -A, --csv, and ON_ERROR_STOP=1, though it lacks a native --json flag (relies on SQL json_agg).

  • Supports -c, --csv, -X, and ON_ERROR_STOP=1; no native --json flag
Trust & Safety 21 / 25

Supports server-enforced PGOPTIONS='-c default_transaction_read_only=on' and --single-transaction rollback safety.

  • Server-enforced default_transaction_read_only=on via PGOPTIONS
Setup & Portability 14 / 15

Pre-installed or trivially available via libpq / postgresql-client on nearly every OS and CI image.

  • Universal OS package availability (libpq, postgresql-client)
Utility & Efficiency 14 / 15

The gold-standard PostgreSQL client for querying, meta-command schema inspection, and COPY streaming.

  • Complete PostgreSQL protocol and meta-command support
Maintenance & Lineage 9 / 10

Official PostgreSQL Global Development Group client maintained for decades.

  • Official postgres/postgres core client
Docs & Verifiability 6 / 10

Exhaustive official man page, though meta-commands and quoting rules have a learning curve.

  • Official docs at postgresql.org/docs/current/app-psql.html

Security, Auth & Guardrails

Auth Modes
Env API Key Config File
Credential Storage

Connection parameters via DATABASE_URL / PGPASSWORD env vars or ~/.pgpass (0600 permissions).

Side Effects
Read-Only Local Write Remote Write Destructive
Cost & Quotas

100% free and open-source under the PostgreSQL License.

Copyable Guardrail for CLAUDE.md / AGENTS.md
# psql Agent Rules
- Always pass `-X -c "<sql>"` (never start interactive `psql` REPL) and set `PAGER=cat`.
- Prefix read queries with `PGOPTIONS='-c default_transaction_read_only=on'`.
- For script execution, always pass `-v ON_ERROR_STOP=1` and `--single-transaction`.

Agent-Ready Command Recipes

Safe Read-Only Queries

Run a guaranteed read-only query as JSON
PGOPTIONS='-c default_transaction_read_only=on' psql "$DATABASE_URL" -X -t -A -c "SELECT coalesce(json_agg(t), '[]'::json) FROM (SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema = 'public' LIMIT 50) t;"

Enforces session-level read-only mode at the server and emits clean JSON via json_agg.

Export query results in clean CSV
PGOPTIONS='-c default_transaction_read_only=on' psql "$DATABASE_URL" -X --csv -c "SELECT now(), version();"

Outputs RFC-4180 CSV without interactive pager or .psqlrc interference.

Controlled Writes & Previews

Apply a reviewed SQL migration file atomically
psql "$DATABASE_URL" -X -v ON_ERROR_STOP=1 --single-transaction -f migration.sql

Executes a migration script inside a single transaction and aborts on first error.

Strengths for Agents

  • + PGOPTIONS='-c default_transaction_read_only=on' enforces true server-side read-only safety.
  • + -v ON_ERROR_STOP=1 --single-transaction prevents half-applied SQL scripts.
  • + --csv and -t -A with json_agg integrate cleanly into shell pipelines.

Caveats & Pitfalls

  • ! No built-in --json output flag; must wrap queries in json_agg or use --csv.
  • ! Invoking psql without -c or -f drops into an interactive REPL and may invoke a pager.

Tested Agent Hosts

Claude Code Codex CLI Gemini CLI