psql Command Reference: Flags, Usage & CI Examples
psql is the PostgreSQL interactive terminal and non-interactive SQL runner.
psql connects to a PostgreSQL server and runs SQL interactively, from a single -c command, or from a -f file. In CI it seeds databases and runs migrations against a Postgres service container.
Common flags and usage
- -h <host> / -p <port>: server host and port (default 5432)
- -U <user> / -d <db>: connect as a role to a database
- -c "SQL": run one command and exit
- -f <file>: run SQL from a file
- -v ON_ERROR_STOP=1: exit non-zero on the first SQL error
- PGPASSWORD env or a postgresql:// URI: supply the password non-interactively
Example
shell
PGPASSWORD=${PGPASSWORD} psql -h localhost -U postgres -d app -v ON_ERROR_STOP=1 -f schema.sql
psql "${DATABASE_URL}" -c 'SELECT 1'In CI
Pass credentials via PGPASSWORD or a connection URI, never interactively. Add -v ON_ERROR_STOP=1 to -f scripts so a failing migration statement actually fails the step instead of exiting 0. Gate psql behind pg_isready so it does not race a half-started service container.
Key takeaways
- psql runs SQL interactively, from -c, or from a -f file.
- Supply the password via PGPASSWORD or a postgresql:// URI in CI.
- ON_ERROR_STOP=1 makes a broken -f migration fail the pipeline.
Related guides
pg_isready Command Reference: Flags, Exit Codes & CI ExamplesReference for pg_isready: -h, -p, -t, -q, the 0/1/2/3 exit codes, and a CI wait loop that blocks until a Post…
pg_dump Command Reference: Flags, Usage & CI ExamplesReference for pg_dump: format flags (-F c/d/t/p), -f, -t, -n, --no-owner, and a CI example that exports a Pos…
createdb Command Reference: Flags, Usage & CI ExamplesReference for createdb: -h, -U, -O, -E, -T, idempotent creation, and a CI example that creates a fresh Postgr…