PostgreSQL (psql) Shortcuts Reference

Comprehensive guide to psql commands and shortcuts

Connection & Session

\c database_name
Connect to a specific database
\c database_name username
Connect to database as specific user
\conninfo
Display current connection information
\q
Quit psql
\! command
Execute shell command

Database Information

\l
List all databases
\l+
List databases with additional details
\du
List all users/roles
\du+
List users with detailed information
\dn
List all schemas
SET search_path TO schema_name;
Change current schema
SHOW search_path;
Show current schema path
\db
List tablespaces

Tables & Schemas

\dt
List all tables in current schema
\dt+
List tables with size and description
\dt schema.*
List tables in specific schema
\d table_name
Describe table structure
\d+ table_name
Describe table with additional details
\di
List all indexes
\dv
List all views
\ds
List all sequences
\df
List all functions
\dT
List all data types
\dd
Show object descriptions/comments
\da
List aggregate functions
\do
List operators
\dD
List domains
\det
List foreign tables
\deu
List user mappings
\dew
List foreign-data wrappers
\des
List foreign servers

Query & Execution

\g
Execute last query again
\p
Show current query buffer
\r
Reset/clear query buffer
\e
Edit query buffer in external editor
\i filename
Execute SQL commands from file
\o filename
Send query results to file
\o
Stop sending output to file
\timing
Toggle timing of queries
\watch [seconds]
Repeat query every N seconds
\copy table FROM 'file.csv' CSV HEADER;
Import CSV data to table
\copy table TO 'file.csv' CSV HEADER;
Export table data to CSV
\COPY (SELECT...) TO 'file.csv' CSV;
Export query results to CSV
\w filename
Write query buffer to file
\ir filename
Execute file relative to current script
\gx
Execute query with expanded output
\gset
Execute query and store results in variables

Output Formatting

\x
Toggle expanded/vertical display
\pset format aligned
Set output format to aligned
\pset format csv
Set output format to CSV
\pset format html
Set output format to HTML
\pset border 2
Set table border style (0-2)
\pset null 'NULL'
Display NULL values as 'NULL'
\pset pager off
Disable pager for output
\H
Toggle HTML output mode

Navigation & History

↑ / ↓
Navigate command history
Ctrl + R
Reverse search in history
Tab
Auto-complete table names, keywords
Ctrl + C
Cancel current command
Ctrl + D
Exit psql (EOF)
Ctrl + L
Clear screen
\s
Display command history
\s filename
Save command history to file

Help & Information

\?
Show all psql commands
\h
Show SQL command syntax help
\h SELECT
Show help for specific SQL command
\dconfig
Show configuration parameters
\dconfig+
Show config parameters with descriptions
SELECT version();
Show PostgreSQL version
\echo message
Print message to stdout
\set
Show all psql variables
\set var_name value
Set psql variable
\unset var_name
Unset psql variable
\prompt text var_name
Prompt user for input and store in variable
\password username
Change user password securely
\encoding
Show current encoding
\encoding UTF8
Set client encoding
Command copied to clipboard!