AI Hints
Home›PostgreSQL Tutorial›Databases, Tables and psql
PostgreSQL · Lesson 6 of 120

psql Meta-commands

Backslash shortcuts.

⏱ 2 min read▶ Video at 4:21🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

Psql has special commands that start with a backslash. They're not SQL. They're shortcuts that describe the database, so you don't have to remember table details.

  • Start with \
  • Not SQL
  • Describe things

Describe a table

Backslash d t lists all the tables. Backslash d, then a table name, describes that table. Run it. Every table, then the staff columns with their types, keys and links.

query.sqlsql
\dt
\d staff
Output
             List of tables
 Schema |    Name     | Type  |  Owner
--------+-------------+-------+----------
 public | books       | table | postgres
 public | customers   | table | postgres
 public | order_items | table | postgres
 public | orders      | table | postgres
 public | reviews     | table | postgres
 public | staff       | table | postgres
(6 rows)

                              Table "public.staff"
   Column   |  Type   | Collation | Nullable |             Default
------------+---------+-----------+----------+----------------------------------
 staff_id   | integer |           | not null | generated by default as identity
 name       | text    |           | not null |
 role       | text    |           | not null |
 manager_id | integer |           |          |
 salary     | integer |           | not null |
Indexes:
    "staff_pkey" PRIMARY KEY, btree (staff_id)
Foreign-key constraints:
    "staff_manager_id_fkey" FOREIGN KEY (manager_id) REFERENCES staff(staff_id)
Referenced by:
    TABLE "staff" CONSTRAINT "staff_manager_id_fkey" FOREIGN KEY (manager_id) REFERENCES
    staff(staff_id)

Handy commands

Backslash l lists databases. Backslash c connects to another one. Backslash d n lists schemas, and backslash d f lists functions. Backslash x switches to expanded output, for wide rows. And backslash q quits.

CommandDoes
\llist databases
\c nameconnect to a database
\dn / \dfschemas / functions
\xexpanded rows
\qquit

Summary

Backslash commands describe your database quickly, and backslash q quits psql. Next, selecting and filtering rows.

Key takeaways
  • \dt
  • \d table
  • \x
Next up: SELECT and WHERE — filter the rows