psql Cheat Sheet

Deep psql reference for connection flags, slash commands, formatting, and automation.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Connecting and Session Variables

Connection parameters, startup flags, and handy variables.

Connect with host port user and database

Connect with explicit connection flags.

bashANYpsqlconnect
bash
psql -h localhost -p 5432 -U appuser -d appdb

See summary for usage details.

Never prompt for password

Fail instead of prompting for a password.

bashANYpsqlconnectpassword
bash
psql -w -h localhost -U appuser -d appdb

See summary for usage details.

Force password prompt

Always prompt for the password interactively.

bashANYpsqlpassword
bash
psql -W -h localhost -U appuser -d appdb

See summary for usage details.

Set psql variable

Create or update a psql variable.

sqlANYpsqlvariables
sql
\set schema_name public

See summary for usage details.

Use psql variable in SQL

Interpolate a psql variable in a command.

sqlANYpsqlvariablesautomation
sql
SELECT * FROM :schema_name.users LIMIT 5;

See summary for usage details.

Show current connection

Display current host, port, user, and database connection info.

sqlANYpsqlconnectioninspect
sql
\conninfo

See summary for usage details.

Use PGPASSFILE

Use a password file for noninteractive authentication.

bashANYpsqlauthautomation
bash
PGPASSFILE=./.pgpass psql -h localhost -U appuser -d appdb

See summary for usage details.

Run one command and exit

Execute a single SQL command from the shell.

bashANYpsqlautomation
bash
psql -d appdb -c "SELECT now();"

See summary for usage details.

Meta-commands and Catalog Navigation

Slash commands to inspect the server quickly.

List tables

List tables in visible schemas.

sqlANYpsqltables
sql
\dt

See summary for usage details.

List views

List views in visible schemas.

sqlANYpsqlviews
sql
\dv

See summary for usage details.

List functions

List functions in the current database.

sqlANYpsqlfunctions
sql
\df

See summary for usage details.

List sequences

List sequences.

sqlANYpsqlsequences
sql
\ds

See summary for usage details.

List access privileges

Show grants and ownership for a relation.

sqlANYpsqlprivilegesinspect
sql
\z public.users

See summary for usage details.

Describe function

Show function details including source and volatility.

sqlANYpsqlfunctiondescribe
sql
\df+ public.set_updated_at

See summary for usage details.

List types

List data types defined in the database.

sqlANYpsqltypes
sql
\dT

See summary for usage details.

List foreign tables

List foreign tables defined via FDWs.

sqlANYpsqlforeign-data-wrapper
sql
\det

See summary for usage details.

Repeat command with watch

Rerun the current query every 5 seconds.

sqlANYpsqlwatchmonitoring
sql
\watch 5

See summary for usage details.

Show meta-command help

List all psql slash commands.

sqlANYpsqlhelp
sql
\?

See summary for usage details.

Formatting and Output Control

Shape result output for terminals, CSV, and scripts.

Unaligned output

Print output without table borders for scripting.

sqlANYpsqlformatting
sql
\pset format unaligned

See summary for usage details.

CSV output

Render result output as CSV.

sqlANYpsqlcsvformatting
sql
\pset format csv

See summary for usage details.

Tuples only

Suppress headers and footers.

sqlANYpsqlformattingscripts
sql
\t on

See summary for usage details.

Set border style

Adjust table border rendering.

sqlANYpsqlformatting
sql
\pset border 2

See summary for usage details.

Set NULL display

Customize how null values are shown.

sqlANYpsqlformattingnull
sql
\pset null '[NULL]'

See summary for usage details.

Write query output to file

Send query output to a file until reset.

sqlANYpsqloutputfile
sql
\o /tmp/query-output.txt

See summary for usage details.

Reset output target

Return output to stdout.

sqlANYpsqloutput
sql
\o

See summary for usage details.

Export query to CSV

Client-side export of a query result to CSV.

sqlANYpsqlcopycsv
sql
\copy (SELECT * FROM public.users ORDER BY user_id) TO '/tmp/users.csv' CSV HEADER

See summary for usage details.

Scripting and Automation

Use psql in repeatable scripts and deployment tasks.

Stop on first error

Exit immediately when a script encounters an error.

bashANYpsqlscriptserror-handling
bash
psql -v ON_ERROR_STOP=1 -d appdb -f deploy.sql

See summary for usage details.

Pass variable on command line

Inject psql variables from the shell.

bashANYpsqlvariablesscripts
bash
psql -v target_schema=public -d appdb -f script.sql

See summary for usage details.

Echo all commands

Print input lines as they are executed.

bashANYpsqlscriptsdebug
bash
psql -a -d appdb -f script.sql

See summary for usage details.

Echo hidden commands

Show underlying SQL generated by backslash commands.

bashANYpsqldebugcatalog
bash
psql -E -d appdb

See summary for usage details.

Execute script in one transaction

Wrap the script in a single transaction.

bashANYpsqlscriptstransaction
bash
psql -1 -d appdb -f migration.sql

See summary for usage details.

Conditional blocks in psql

Use psql conditionals in interactive or scripted sessions.

sqlANYpsqlconditionalsautomation
sql
\if :{?target_schema}
  \echo 'target schema set'
\else
  \echo 'target schema missing'
\endif

See summary for usage details.

Include another script

Read and execute another SQL file.

sqlANYpsqlincludescripts
sql
\i ./common.sql

See summary for usage details.

Run shell command from psql

Execute a shell command from inside psql.

sqlANYpsqlshellutility
sql
\! date

See summary for usage details.

Recommended next

No recommendations yet.