SQL module
Lesson 2
psql and the catalog
Use psql meta-commands to explore tables, columns, and connection settings.
beginner25 minRun on your machine
What you will be able to do
- Connect with psql using host port 15432
- List tables and describe a table with \dt and \d
- Run a one-shot query with psql -c
Why this matters for data work
Before writing a clever query, you look at what exists. psql meta-commands (\d, \dt) are the warehouse equivalent of ls and cat on schemas.
Concepts
| Command | Meaning |
|---|---|
\dt |
list tables |
\d orders |
describe columns and keys |
\x |
toggle expanded display |
\q |
quit |
-c "SQL" |
one shot from the shell |
Practice on your machine
export PGPASSWORD=dcubes PGHOST=127.0.0.1 PGPORT=15432 PGUSER=dcubes PGDATABASE=retail
psql
Inside psql:
\dt
\d orders
\d stores
SELECT store_id, store_name FROM stores ORDER BY store_id;
\q
One-shot:
psql -c "\dt"
psql -c "SELECT category, count(*) FROM products GROUP BY 1 ORDER BY 1;"
What you should see
- Three tables:
orders,products,stores. orderscolumns matching the CSV header.- Five stores when you SELECT.
Common mistakes
- Forgetting PGPASSWORD — psql prompts or fails.
- Wrong database —
\c retailif you landed elsewhere. - Typing SQL meta as SQL —
\dtis notSELECT \dt.