PostgreSQL psql Commands

psql is the text-based terminal program that comes with PostgreSQL. You type a line, press Enter, and the server sends back an answer. Think of psql as a walkie-talkie between you and the database: you speak, the database replies, and nothing fancy sits in between.

  You (keyboard)  ---->  psql  ---->  PostgreSQL Server
                                            |
  Screen (results) <----  psql  <-----------+

Graphical tools exist, but psql runs on every server, every laptop, and every cloud machine. Learning it once gives you a skill that works everywhere.

Connecting to a Database

A connection needs four details: the host, the port, the user name, and the database name. The command below connects to a database named bakery on your own computer.

psql -h localhost -p 5432 -U postgres -d bakery
FlagMeaningDefault
-hHost name or address of the serverlocalhost
-pPort number5432
-UUser nameYour system user
-dDatabase nameSame as the user name

After a successful login, the prompt changes to bakery=#. The name before the symbol tells you which database you are working in. A # means you are a superuser. A > means you are a regular user.

Two Kinds of Commands

psql understands two families of input. Mixing them up causes most beginner confusion.

SQL Statements

These go to the server. They must end with a semicolon. Without the semicolon, psql waits for more text and shows a prompt like bakery-#.

SELECT current_date;

Meta-Commands

These talk to psql itself. They start with a backslash and need no semicolon. The server never sees them.

  SELECT ...;     --> travels to the server
  \dt             --> stays inside psql

Everyday Meta-Commands

CommandWhat It Does
\lLists all databases
\c dbnameSwitches to another database
\dtLists tables in the current schema
\d tablenameShows columns, types, and indexes of one table
\dnLists schemas
\duLists roles and users
\dfLists functions
\dvLists views
\timingTurns query timing on or off
\xTurns expanded display on or off
\i file.sqlRuns the commands stored in a file
\o file.txtSends query output to a file
\?Shows help for all meta-commands
\h SELECTShows help for an SQL statement
\qQuits psql

Using psql Effectively

Exploration, display options, and script files make psql a complete daily tool.

Exploring a Database Step by Step

Imagine you join a bakery business and receive access to its database. You know nothing about it. Four commands reveal the whole picture.

\l
\c bakery
\dt
\d orders

The first command lists every database. The second moves you into bakery. The third shows the tables, and the fourth shows the structure of the orders table. In under a minute, you know what the database holds.

Expanded Display

A table with many columns wraps across the screen and becomes unreadable. Expanded display turns each row into a small vertical card.

  Normal view:    id | name | price | stock | created_at | ...
  Expanded view:  id         | 1
                  name       | Sourdough
                  price      | 4.50

Type \x to switch it on. Type \x again to switch it off. Use \x auto to let psql pick the best style for each result.

Measuring Query Speed

Type \timing once. Every later statement reports its run time in milliseconds. This habit helps you spot slow queries long before users complain.

Running Script Files

Store long commands in a text file and run them with \i. A file named setup.sql that creates tables and inserts sample rows runs with one line.

\i /home/user/setup.sql

Run a file straight from the operating system shell without opening psql first.

psql -d bakery -f setup.sql

Saving Results to a File

The \o command redirects output. Run \o report.txt, execute a query, and run \o with no name to return output to the screen. The file now holds the query result.

Useful Habits

  • Press the Up arrow to recall earlier commands.
  • Press Tab to complete table and column names.
  • Use \e to open the last query in a text editor.
  • Check the prompt before running DELETE or DROP, so you know which database you are in.

Practice Exercise

Connect to any database you own. List all databases with \l. Switch to one with \c. List its tables with \dt. Pick one table, inspect it with \d, and turn on \timing before you run a SELECT query on it.