Skip to content
CLI tool · Databases

psql for AI agents

psql, the PostgreSQL terminal, for coding agents: list tables, describe schemas, EXPLAIN queries, and run read-only sessions safely against Postgres.

psqlby PostgreSQL Global Development Group
What it is

What psql does for an agent

psql is the official interactive terminal for PostgreSQL. Besides running SQL, it has backslash meta-commands for listing tables (\dt), describing a table (\d), and more.

With -c for a single command, -X to skip personal startup files, and ON_ERROR_STOP for scripts, it works well non-interactively, which is how agents should use it.

Official site: www.postgresql.org

Install

Install psql

The install commands from the official docs. Check it worked with psql --version.

macOS (Homebrew, client only)
brew install libpq
Debian or Ubuntu (client only)
sudo apt-get install postgresql-client
Auth

Signing in psql for an agent

psql connects with a connection string or the standard libpq environment variables, and can read passwords from ~/.pgpass. Give agents a role with read-only grants, not the owner or superuser.

Connect with a URL from the environment
psql "$DATABASE_URL"

Environment variables: DATABASE_URL, PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE. Set them in the agent's environment; never paste values into prompts or commit them.

Common agent tasks

What agents use psql for

Example commands with placeholder names. Swap in your own project, app, or file names.

  1. List tables

    psql
    psql "$DATABASE_URL" -X -c '\dt'
  2. Describe a table

    psql
    psql "$DATABASE_URL" -X -c '\d users'
  3. Run a query in a read-only session

    psql
    PGOPTIONS='-c default_transaction_read_only=on' psql "$DATABASE_URL" -X -c 'SELECT count(*) FROM users'
  4. Check a query plan

    psql
    psql "$DATABASE_URL" -X -c 'EXPLAIN SELECT * FROM orders WHERE customer_id = 42'

    EXPLAIN ANALYZE executes the query; use plain EXPLAIN for anything that writes.

  5. Run a migration file and stop on the first error

    psql
    psql "$DATABASE_URL" -X -v ON_ERROR_STOP=1 -f migrations/001_init.sql

    Writes to the database. Local or disposable databases only, unless approved.

Safety

Safety rules for agents

Safe to run without asking

  • \dt, \d table, \dn
  • SELECT queries in a read-only session
  • EXPLAIN (without ANALYZE on writes)

Ask a human first

  • INSERT, UPDATE, DELETE
  • CREATE, ALTER, DROP, TRUNCATE
  • psql -f with a migration against a shared database
  • GRANT or REVOKE
  • Connect with a read-only role, or force read-only sessions with default_transaction_read_only.
  • Add a LIMIT to exploratory queries on large tables.
  • Never run DROP, TRUNCATE, or a DELETE or UPDATE without a WHERE clause.
  • Never print the connection string; it usually contains the password.
Agent instructions

AGENTS.md or CLAUDE.md snippet

Paste this into the instruction file your agent reads at the start of every session, then adjust the rules to your setup.

AGENTS.md
## psql

Use `psql` for inspecting PostgreSQL schemas, running read-only queries, and checking query plans.

- Check it is installed with `psql --version`. If it is missing, ask before installing it.
- Auth: credentials are set up by a human. Never print or commit them (DATABASE_URL, PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE).
- Run without asking (read-only): `\dt, \d table, \dn`, `SELECT queries in a read-only session`, `EXPLAIN (without ANALYZE on writes)`.
- Ask a human first (changes or deletes things): `INSERT, UPDATE, DELETE`, `CREATE, ALTER, DROP, TRUNCATE`, `psql -f with a migration against a shared database`, `GRANT or REVOKE`.
- Connect with a read-only role, or force read-only sessions with default_transaction_read_only.
- Add a LIMIT to exploratory queries on large tables.
- Never run DROP, TRUNCATE, or a DELETE or UPDATE without a WHERE clause.
- Never print the connection string; it usually contains the password.
- Prefer JSON output where the command supports it, and keep output short.
CLI or MCP server?

psql or an MCP server?

No official MCP server. PostgreSQL itself has no official MCP server, but several database vendors and community projects publish Postgres servers. They help in chat clients without a shell; in a terminal agent, psql with a read-only role is often enough.

Rule of thumb: in a terminal agent that a human has signed in, the CLI is usually faster and cheaper in context. In a chat client without a shell, or when you want a fixed set of typed tools, use an MCP server.

Same category
All Databases tools
FAQ

psql questions

How do I install psql?

macOS (Homebrew, client only): brew install libpq. Other official options: Debian or Ubuntu (client only). Check the install with psql --version, and see the PostgreSQL Global Development Group docs for other platforms.

How does an AI agent authenticate psql?

psql connects with a connection string or the standard libpq environment variables, and can read passwords from ~/.pgpass. Give agents a role with read-only grants, not the owner or superuser.

Which psql commands are safe for an agent to run?

Commands that only read state are safe to run without asking: \dt, \d table, \dn; SELECT queries in a read-only session; EXPLAIN (without ANALYZE on writes). Ask a human before anything that changes or deletes something: INSERT, UPDATE, DELETE; CREATE, ALTER, DROP, TRUNCATE; psql -f with a migration against a shared database; GRANT or REVOKE.

Is there an MCP server for psql?

PostgreSQL itself has no official MCP server, but several database vendors and community projects publish Postgres servers. They help in chat clients without a shell; in a terminal agent, psql with a read-only role is often enough.