1. Executing SQL Files
psql -d shop -f schema.sql
\i /path/to/script.sql -- inside psql
\ir relative.sql -- relative to current script
2. Listing Databases (\l, \list)
\l
\l+ -- with size, tablespace, description
3. Listing Tables
\dt
\dt+ public.*
\dtS -- include system tables
4. Listing Schemas
5. Listing Views
\dv
\dm -- materialized views
6. Listing Functions
7. Describing Table
\d orders
\d+ orders -- columns, indexes, constraints, triggers, storage
8. Viewing Permissions
\dp orders
\z orders -- alias of \dp
\ddp -- default privileges
9. Listing Indexes
10. Listing Sequences
11. Toggling Expanded Display
\x -- toggle
\x auto -- auto for wide rows
\pset format aligned|unaligned|csv|json|html|wrapped
\pset border 2
\pset null '(null)'
13. Timing Commands
14. Editing in External Editor
\e -- edit query buffer in $EDITOR
\ef function_name -- edit function definition
\ev view_name -- edit view definition
15. Using Variables
\set tid 42
SELECT * FROM orders WHERE customer_id = :tid;
\set quoted '\'foo\''
\echo :tid
| Variable | Detail |
| :name | Unquoted substitution |
| :'name' | Quoted string |
| :"name" | Quoted identifier |