Using psql Meta-Commands

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

\dn
\dn+

5. Listing Views

\dv
\dm     -- materialized views

6. Listing Functions

\df
\df+ schema.fn*

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

\di
\di+ public.*

10. Listing Sequences

\ds

11. Toggling Expanded Display

\x          -- toggle
\x auto     -- auto for wide rows

12. Setting Output Format

\pset format aligned|unaligned|csv|json|html|wrapped
\pset border 2
\pset null '(null)'

13. Timing Commands

\timing on

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
VariableDetail
:nameUnquoted substitution
:'name'Quoted string
:"name"Quoted identifier