PostgreSQL psql Shortcut Commands
Table of Contents
1. Built-in Help: What to Do When You Don’t Know the Full Command (\? and \h)
You never need to memorize every SQL syntax or psql flag in PostgreSQL. The interactive terminal has a complete offline manual built right into it:
β’
β’
β’
β’
\? β Lists all psql backslash shortcut commands (like \l, \dt, \du).β’
\h β Lists all SQL commands.β’
\h COMMAND_NAME β Shows the exact syntax for a specific SQL command (even if you only know part of it!).β’
TAB Key β Press the TAB key twice while typing any SQL command, table name, or backslash command for automatic completion.-- 1. Forgot the exact syntax for ALTER USER? Just type \h ALTER USER:
gebua=# \h ALTER USER
Command: ALTER USER
Description: change a database role
Syntax:
ALTER USER role_specification [ WITH ] option [ ... ]
where option can be:
SUPERUSER | NOSUPERUSER
| CREATEDB | NOCREATEDB
| CREATEROLE | NOCREATEROLE
| INHERIT | NOINHERIT
| LOGIN | NOLOGIN
| REPLICATION | NOREPLICATION
| BYPASSRLS | NOBYPASSRLS
| CONNECTION LIMIT connlimit
| [ ENCRYPTED ] PASSWORD 'password' | PASSWORD NULL
| VALID UNTIL 'timestamp'
-- 2. Only know the word "TABLESPACE" or "SCHEMA" and want to see all related commands?
gebua=# \h *TABLESPACE*
Command: ALTER TABLESPACE
Description: change the definition of a tablespace
...
Command: CREATE TABLESPACE
Description: define a new tablespace
...
Command: DROP TABLESPACE
Description: remove a tablespace
-- 3. Need help with psql backslash commands?
gebua=# \?
General
\copyright show PostgreSQL usage and distribution terms
\g [FILE] or ; execute query (and send results to file or |pipe)
\h [NAME] help on syntax of SQL commands, * for all commands
\q quit psql
...
2. The Golden Rule of Modifiers: Adding “+” and “S” to Any Shortcut
Almost every \d or \l command in PostgreSQL supports two universal modifiers and pattern wildcards:
| Modifier | Meaning | Example |
|---|---|---|
| + | Extended Details: Appends object size on disk, tablespace, access privileges (ACLs), and comments/descriptions. | \l+, \dt+, \db+, \dn+ |
| S | System Objects: Includes internal PostgreSQL system catalogs (pg_catalog, information_schema, pg_toast). |
\dnS, \dtS, \dfS, \dvS |
| * (Wildcard) | Pattern Matching: Filters objects by schema or partial name. | \dt geb.*, \dt *emp* |
3. Connection & OS Navigation Shortcuts (\c, \conninfo, \!, \q)
-- Check current database, user, socket/IP, and port:
gebua=# \conninfo
You are connected to database "gebua" as user "postgres" via socket in "/run/postgresql" at port "5432".
-- Switch database and/or user (\c database_name user_name):
gebua=# \c gebua gebadm
You are now connected to database "gebua" as user "gebadm".
-- Run a Linux OS command without leaving psql (\!):
gebua=> \! df -h /pgData
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 12G 38G 24% /pgData
-- Check current working directory or change directory:
gebua=> \! pwd
/var/lib/pgsql
gebua=> \cd /tmp
-- Quit psql:
gebua=> \q
4. Database, Tablespace & Schema Shortcuts (\l, \db, \dn)
-- 1. List all databases (\l) and with sizes/descriptions (\l+):
gebua=# \l+ gebua
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Access privileges | Size | Tablespace | Description
-------+----------+----------+-----------------+-------------+-------------+-------------------+---------+------------+-------------
gebua | postgres | UTF8 | libc | en_SG.UTF-8 | en_SG.UTF-8 | | 7921 kB | pg_default |
(1 row)
-- 2. List all tablespaces (\db) and with disk location & size (\db+):
gebua=# \db+
List of tablespaces
Name | Owner | Location | Access privileges | Options | Size | Description
------------+----------+----------+-------------------+---------+--------+-------------
pg_default | postgres | | | | 31 MB |
pg_global | postgres | | | | 595 kB |
(2 rows)
-- 3. List schemas (\dn), system schemas (\dnS), and with privileges (\dn+):
gebua=# \dn+
List of schemas
Name | Owner | Access privileges | Description
--------+-------------------+----------------------------------------+------------------------
geb | gebadm | gebadm=UC/gebadm +|
public | pg_database_owner | pg_database_owner=UC/pg_database_owner+| standard public schema
(2 rows)
5. Users, Roles & Privilege Shortcuts (\du, \dp, \z, \ddp)
-- 1. List all users/roles (\du) or a specific role (\du role_name):
gebua=# \du geb*
List of roles
Role name | Attributes | Member of
-----------+--------------+-----------
geb_ro | Cannot login | {}
geb_rw | Cannot login | {}
gebadm | | {}
gebusr | | {geb_ro}
-- 2. Check Table/Sequence Access Privileges (\dp or \z β both are identical):
gebua=# \dp geb.emp
Access privileges
Schema | Name | Type | Access privileges | Column privileges | Policies
--------+------+-------+------------------------+-------------------+----------
geb | emp | table | gebadm=arwdDxtm/gebadm+| |
| | | geb_ro=r/gebadm +| |
| | | geb_rw=arwd/gebadm | |
(1 row)
-- 3. Check Default Privileges configured for future objects (\ddp):
gebua=# \ddp
Default access privileges
Owner | Schema | Type | Access privileges
--------+--------+----------+---------------------------
gebadm | geb | sequence | geb_ro=rU/gebadm +
| | | geb_rw=rU/gebadm
gebadm | geb | table | geb_ro=r/gebadm +
| | | geb_rw=arwd/gebadm
(2 rows)
6. Tables, Views, Indexes, Sequences & Functions (\d, \dt, \dv, \di, \ds, \df)
-- 1. Describe a specific table's columns, types, indexes, and foreign keys (\d or \d+):
gebua=# \d geb.emp
Table "geb.emp"
Column | Type | Collation | Nullable | Default
----------+------------------------+-----------+----------+-------------------------------------
emp_id | integer | | not null | nextval('geb.emp_emp_id_seq'::regclass)
emp_name | character varying(100) | | not null |
dept_id | integer | | |
salary | numeric(10,2) | | |
Indexes:
"emp_pkey" PRIMARY KEY, btree (emp_id)
Foreign-key constraints:
"emp_dept_id_fkey" FOREIGN KEY (dept_id) REFERENCES geb.dept(dept_id)
-- 2. List ONLY tables in a schema with their disk sizes (\dt+):
gebua=# \dt+ geb.*
List of relations
Schema | Name | Type | Owner | Persistence | Access method | Size | Description
--------+---------+-------+--------+-------------+---------------+------------+-------------
geb | dept | table | gebadm | permanent | heap | 8192 bytes |
geb | emp | table | gebadm | permanent | heap | 8192 bytes |
geb | payroll | table | gebadm | permanent | heap | 8192 bytes |
(3 rows)
-- 3. List Indexes (\di+), Sequences (\ds+), Views (\dv+), Materialized Views (\dm+):
gebua=# \di+ geb.*
List of relations
Schema | Name | Type | Owner | Table | Persistence | Access method | Size | Description
--------+--------------+-------+--------+---------+-------------+---------------+--------+-------------
geb | dept_pkey | index | gebadm | dept | permanent | btree | 16 kB |
geb | emp_pkey | index | gebadm | emp | permanent | btree | 16 kB |
geb | payroll_pkey | index | gebadm | payroll | permanent | btree | 16 kB |
(3 rows)
gebua=# \ds+ geb.*
List of relations
Schema | Name | Type | Owner | Persistence | Type | Size | Description
--------+------------------------+----------+--------+-------------+------------+------------+-------------
geb | dept_dept_id_seq | sequence | gebadm | permanent | integer | 8192 bytes |
geb | emp_emp_id_seq | sequence | gebadm | permanent | integer | 8192 bytes |
geb | payroll_payroll_id_seq | sequence | gebadm | permanent | integer | 8192 bytes |
(3 rows)
7. Query Formatting, Timing, Auto-Refresh & Scripting (\x, \timing, \watch, \i, \o)
These power-user shortcuts make daily troubleshooting and DBA scripting much easier:
-- 1. Toggle Vertical / Expanded Display (\x or \x auto) β best for wide tables like pg_stat_activity:
gebua=# \x
Expanded display is on.
gebua=# SELECT * FROM geb.emp WHERE emp_id = 1;
-[ RECORD 1 ]------
emp_id | 1
emp_name | Arjun
dept_id | 1
salary | 75000.00
gebua=# \x
Expanded display is off.
-- 2. Show execution time in milliseconds for every query (\timing):
gebua=# \timing
Timing is on.
gebua=# SELECT count(*) FROM geb.emp;
count
-------
3
(1 row)
Time: 0.482 ms
-- 3. Auto-refresh a query every N seconds (\watch N) β awesome for live monitoring!
gebua=# SELECT pid, usename, state, query FROM pg_stat_activity WHERE state = 'active' \watch 2
-- 4. Spool query output to a file (\o filename) and stop spooling (\o):
gebua=# \o /tmp/emp_report.txt
gebua=# SELECT * FROM geb.emp;
gebua=# \o
-- 5. Execute an external SQL script file (\i filename.sql):
gebua=# \i /tmp/create_tables.sql
8. Master psql Meta-Commands Cheat Sheet
| Category | Command | What It Does |
|---|---|---|
| Help | \? |
List all psql backslash commands |
\h [SQL_COMMAND] |
Show syntax help for SQL commands (e.g., \h GRANT, \h *USER*) |
|
\set ECHO_HIDDEN on |
Display the underlying catalog SQL query executed by any \d command |
|
| Connection & OS | \c [db] [user] |
Connect to a new database and/or switch user |
\conninfo |
Display current database, user, host/socket, and port | |
\! [command] |
Execute Linux shell command without leaving psql (e.g., \! ls -l) |
|
\q |
Quit / exit psql | |
| Databases, Storage & Schemas | \l or \l+ |
List all databases (+ adds database size, tablespace, and description) |
\db or \db+ |
List all tablespaces (+ adds disk path location and size) |
|
\dn / \dn+ / \dnS |
List schemas (+ adds privileges, S shows system schemas) |
|
| Users, Roles & Security | \du or \du+ |
List all roles/users and their attributes & group memberships |
\dp [pattern] or \z |
List table, view, and sequence access privileges (ACLs) | |
\ddp |
List default privileges configured via ALTER DEFAULT PRIVILEGES |
|
\password [user] |
Securely change password for a user (hashes client-side) | |
| Database Objects | \d [object] / \d+ |
Describe table/view/index/sequence columns, constraints, and triggers |
\dt [pattern] / \dt+ |
List tables (e.g., \dt geb.* or \dt *.* for all schemas) |
|
\di [pattern] / \di+ |
List indexes | |
\dv [pattern] / \dv+ |
List views | |
\dm [pattern] / \dm+ |
List materialized views | |
\ds [pattern] / \ds+ |
List sequences | |
\df [pattern] / \df+ |
List functions / procedures | |
\dE [pattern] |
List foreign tables | |
\dx / \dx+ |
List installed extensions | |
| Formatting & Scripting | \x or \x auto |
Toggle expanded vertical output format (like MySQL \G) |
\timing |
Toggle display of query execution time in milliseconds | |
\watch [sec] |
Re-run the current query every N seconds (default 2s) | |
\i filename.sql |
Execute SQL commands from an external file | |
\o [filename] |
Spool/save all query outputs to a file (run \o alone to stop) |
|
\e |
Open the current query buffer in your default editor (vi/nano) |
Caution: Your use of any information or materials on this website is entirely at your own risk. It is provided for educational purposes only. It has been tested internally, however, we do not guarantee that it will work for you. Ensure that you run it in your test environment before using.
Thank you
Rajasekhar Amudala