psql basics
psql is PostgreSQL's command-line client: you type SQL and shortcuts beginning with a backslash.
In this page:
Syntax
psql -U username -d database_name -h host
\l
\c database_name
\dt
\d table_name
\q
psql basics
Connect with psql -U user -d database -h host. Useful meta-commands are \l (list databases), \c name (connect), \dt (list tables), \d table (describe a table), \du (roles), \x (expanded display), \i file.sql (run a file), \timing and \q (quit).
SQL statements end with a semicolon.
Note:
Type \? for the full list of psql commands.
Example: psql basics
$ psql -U postgres
postgres=# \l
postgres=# \c shop
You are now connected to database "shop" as user "postgres".
shop=# \dt
List of relations
Schema | Name | Type | Owner
--------+-----------+-------+----------
public | customers | table | postgres
shop=# \d customers
shop=# \q
⚠️ Run this in your own terminal or Node.js environment.
Related Topics
Common Mistakes
- Forgetting the semicolon at the end of SQL
- Using SQL for meta-commands
- Typing \d without a table name and getting a long list
Chapter Summary
- psql is the interactive client
- Meta-commands start with a backslash
- \l \c \dt \d are the basics
- SQL ends with a semicolon
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: