psql -h localhost -p 5432 -U appuser -d appdbSee summary for usage details.
Deep psql reference for connection flags, slash commands, formatting, and automation.
Connection parameters, startup flags, and handy variables.
psql -h localhost -p 5432 -U appuser -d appdbSee summary for usage details.
psql -w -h localhost -U appuser -d appdbSee summary for usage details.
psql -W -h localhost -U appuser -d appdbSee summary for usage details.
\set schema_name publicSee summary for usage details.
SELECT * FROM :schema_name.users LIMIT 5;See summary for usage details.
Display current host, port, user, and database connection info.
\conninfoSee summary for usage details.
PGPASSFILE=./.pgpass psql -h localhost -U appuser -d appdbSee summary for usage details.
psql -d appdb -c "SELECT now();"See summary for usage details.
Shape result output for terminals, CSV, and scripts.
\pset format unalignedSee summary for usage details.
\pset format csvSee summary for usage details.
\t onSee summary for usage details.
\pset border 2See summary for usage details.
\pset null '[NULL]'See summary for usage details.
\o /tmp/query-output.txtSee summary for usage details.
\oSee summary for usage details.
\copy (SELECT * FROM public.users ORDER BY user_id) TO '/tmp/users.csv' CSV HEADERSee summary for usage details.
Use psql in repeatable scripts and deployment tasks.
Exit immediately when a script encounters an error.
psql -v ON_ERROR_STOP=1 -d appdb -f deploy.sqlSee summary for usage details.
psql -v target_schema=public -d appdb -f script.sqlSee summary for usage details.
psql -a -d appdb -f script.sqlSee summary for usage details.
Wrap the script in a single transaction.
psql -1 -d appdb -f migration.sqlSee summary for usage details.
Use psql conditionals in interactive or scripted sessions.
\if :{?target_schema}
\echo 'target schema set'
\else
\echo 'target schema missing'
\endifSee summary for usage details.
\i ./common.sqlSee summary for usage details.
\! dateSee summary for usage details.