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
Connect with host port user and database
psql -h localhost -p 5432 -U appuser -d appdb

# Connect with explicit connection flags.

Never prompt for password
psql -w -h localhost -U appuser -d appdb

# Fail instead of prompting for a password.

Force password prompt
psql -W -h localhost -U appuser -d appdb

# Always prompt for the password interactively.

Set psql variable
\set schema_name public

# Create or update a psql variable.

Use psql variable in SQL
SELECT * FROM :schema_name.users LIMIT 5;

# Interpolate a psql variable in a command.

Show current connection
\conninfo

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

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

# Use a password file for noninteractive authentication.

Run one command and exit
psql -d appdb -c "SELECT now();"

# Execute a single SQL command from the shell.

## Meta-commands and Catalog Navigation
List tables
\dt

# List tables in visible schemas.

List views
\dv

# List views in visible schemas.

List functions
\df

# List functions in the current database.

List sequences
\ds

# List sequences.

List access privileges
\z public.users

# Show grants and ownership for a relation.

Describe function
\df+ public.set_updated_at

# Show function details including source and volatility.

List types
\dT

# List data types defined in the database.

List foreign tables
\det

# List foreign tables defined via FDWs.

Repeat command with watch
\watch 5

# Rerun the current query every 5 seconds.

Show meta-command help
\?

# List all psql slash commands.

## Formatting and Output Control
Unaligned output
\pset format unaligned

# Print output without table borders for scripting.

CSV output
\pset format csv

# Render result output as CSV.

Tuples only
\t on

# Suppress headers and footers.

Set border style
\pset border 2

# Adjust table border rendering.

Set NULL display
\pset null '[NULL]'

# Customize how null values are shown.

Write query output to file
\o /tmp/query-output.txt

# Send query output to a file until reset.

Reset output target
\o

# Return output to stdout.

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

# Client-side export of a query result to CSV.

## Scripting and Automation
Stop on first error
psql -v ON_ERROR_STOP=1 -d appdb -f deploy.sql

# Exit immediately when a script encounters an error.

Pass variable on command line
psql -v target_schema=public -d appdb -f script.sql

# Inject psql variables from the shell.

Echo all commands
psql -a -d appdb -f script.sql

# Print input lines as they are executed.

Echo hidden commands
psql -E -d appdb

# Show underlying SQL generated by backslash commands.

Execute script in one transaction
psql -1 -d appdb -f migration.sql

# Wrap the script in a single transaction.

Conditional blocks in psql
\if :{?target_schema}
  \echo 'target schema set'
\else
  \echo 'target schema missing'
\endif

# Use psql conditionals in interactive or scripted sessions.

Include another script
\i ./common.sql

# Read and execute another SQL file.

Run shell command from psql
\! date

# Execute a shell command from inside psql.

Recommended next

No recommendations yet.