psql is the interactive terminal for PostgreSQL. Meta-commands (backslash) control the client; SQL statements end with ; and talk to the server.
Connection and help
-- From shell:
psql postgresql://user:pass@localhost:5432/practice
-- Inside psql:
\? -- help on backslash commands
\h SELECT -- SQL command help
\conninfoPractice: Run in psql or DB Fiddle (PostgreSQL). Use your practice database from the workflow lesson.
Inspecting objects
\l -- databases
\dn -- schemas
\dt -- tables in search_path
\d+ orders -- detailed table info
\di -- indexes
Running scripts and output
\i /path/to/seed.sql -- execute file
\o results.txt -- send query output to file
\x on -- expanded display for wide rows
\copy customers TO 'customers.csv' CSV HEADER\copy is a client-side bulk load/unload—distinct from server COPY which requires server filesystem access.
Important interview questions and answers
- Q: \copy vs COPY?
A: \copy streams through the client; COPY runs on the server with file path permissions on the host. - Q: How to quit psql?
A: \q or Ctrl+D.
Self-check
- Which command shows table column types?
- What ends a SQL statement in psql?
Pitfall: Forgetting the semicolon after SQL—psql waits until you terminate the statement.
Interview prep
- \d purpose?
Describes table columns, types, defaults, and indexes.
- \copy vs COPY?
\copy is client-side; COPY runs on server filesystem.