Skip to content

psql — PostgreSQL CLI for AI Agents

Let your AI agent query, migrate, and manage the world's most advanced open-source database

Browse all CLI tools for AI agents

What your agent can do

Your database migration failed halfway. Some tables have the new columns, others don't. You open pgAdmin, try to figure out what applied and what didn't, compare schemas between environments manually. Your agent runs `psql -c "\d users"` to see the exact schema, compares it to the migration file, and tells you which statements need to be re-run. Thirty seconds instead of thirty minutes.psql is the official command-line interface for PostgreSQL, the database behind Supabase, Neon, AWS RDS, Railway, and millions of production applications. Every psql command works identically across all of them. Your agent stores a `DATABASE_URL`, connects with `psql $DATABASE_URL`, and has full access to query data, inspect schemas, run migrations, and export results.The output format flexibility is what makes psql genuinely agent-native. `\pset format json` turns query results into JSON arrays of objects. `\pset format csv` for spreadsheet-ready data. `COPY ... TO STDOUT WITH CSV HEADER` for bulk exports that pipe directly to files or other tools. Your agent picks the format that matches the downstream consumer and gets clean, parseable data every time.Scripting is the real separation from GUI tools. Your agent runs migration files with `psql -f migration.sql -v ON_ERROR_STOP=1 --single-transaction`. That's a single transaction that rolls back completely if any statement fails. Without `ON_ERROR_STOP=1`, psql continues after errors, leaving your database half-migrated. Without `--single-transaction`, each statement commits independently, making rollback impossible. These two flags together are the difference between safe migrations and production incidents.The insider detail: `-X -t -A`. Three flags that turn psql from a human-friendly terminal into an agent-native data pipe. `-X` skips `.psqlrc` (no custom prompts or pagers breaking output). `-t` strips column headers and row counts. `-A` removes alignment padding. Together, `psql -XtAc "SELECT email FROM users WHERE active"` returns one clean value per line. No decoration, no parsing needed.

Frequently asked questions

Can AI agents use psql to manage databases?
Yes. psql runs fully non-interactive with `-c` for commands and `-f` for SQL files. Your agent outputs data as JSON (`\pset format json`), CSV, or HTML. The `-X` flag ensures deterministic output by skipping user configuration. PostgreSQL also has official MCP servers — `@modelcontextprotocol/server-postgres` for read-only access and `crystaldba/postgres-mcp` for read-write with performance analysis. Your agent connects to Supabase, Neon, AWS RDS, or any PostgreSQL instance with a connection string. Install psql with `brew install libpq` on macOS or `sudo apt install postgresql-client` on Linux.
What is the difference between psql and Supabase CLI?
Different tools for different jobs. Supabase CLI manages the Supabase platform — local dev stack, Edge Functions, type generation, and migration workflows. psql is the universal PostgreSQL client that connects to any PostgreSQL database, including Supabase. Your agent uses Supabase CLI for platform operations and psql for direct SQL access. They complement each other: `supabase db diff` generates migrations, psql applies them. `supabase gen types` needs the schema that psql can inspect and verify.
Does psql support JSON output?
Yes, two ways. First, `\pset format json` switches all query output to JSON arrays of objects. Second, PostgreSQL's built-in functions — `row_to_json()`, `json_agg()`, `to_jsonb()` — shape JSON directly in SQL. Your agent uses `\pset format json` for ad-hoc exploration and SQL-level JSON functions when the output structure matters. Combined with `-t` (no headers) and `-A` (no padding), the output is clean machine-readable JSON.