Getting Started with PostgreSQLLesson 1.2
What is psql and how to use the psql command line tool
psql meta-commands, \l \c \dt \d, connecting to a database, quitting psql, SQL terminator
The psql CLI
psql is the official PostgreSQL interactive terminal. You will use it constantly — for running queries, inspecting schemas, and scripting migrations.
Connecting
psql -U postgres -d mydb -h localhost -p 5432Flags: -U user, -d database, -h host, -p port. If you omit -d, psql connects to a database matching your username.
Key meta-commands
\l -- list all databases
\c mydb -- connect to a database
\dt -- list tables in current schema
\d users -- describe a table
\i file.sql -- execute a SQL file
\q -- quitMeta-commands start with a backslash and do not need a semicolon. Regular SQL statements must end with ; — psql buffers input until it sees one.
Useful shortcuts
\timing on -- show query execution time
\x -- toggle expanded output (wide rows)
\e -- open query in $EDITORUse \? to list all meta-commands and \h SELECT to read the SQL syntax for any command. For scripting, pass -c for a single command or -f for a file: psql -U postgres -d mydb -f migration.sql.
