Free lesson · What a Database Is, and Your First Query

Setting up: PostgreSQL and the practice database

You need two things: PostgreSQL running on your machine, and a way to type queries at it. Everything in this course is tested against PostgreSQL 14 or later.

Installing PostgreSQL

  • macOS — install Postgres.app, or brew install postgresql@16 then brew services start postgresql@16.
  • Windows — download the installer from postgresql.org. Accept the defaults; note the password you set for the postgres user.
  • Linuxsudo apt install postgresql (Debian/Ubuntu) or your distribution's equivalent.

Check it worked by opening a terminal and running:

psql --version

If that prints a version number, you have a working install and the psql command-line client.

Loading the practice database

Download practice.sql from the next lesson, then from the folder you saved it in:

createdb practice
psql practice -f practice.sql

The last thing it prints should be a small table of counts:

 authors | books | customers | orders | order_items
---------+-------+-----------+--------+-------------
      10 |    20 |        15 |     25 |          36

If you see those five numbers, you are ready. If you see errors instead, the most common cause is running the command from a different folder than the file is in.

Getting a prompt

psql practice

You will get a prompt like practice=#. Type this and press Enter:

SET search_path TO shop;

Our tables live in a schema called shop — a namespace inside the database, so these tables cannot collide with anything else you create later. That SET tells Postgres to look in shop without you writing shop.books every time. It lasts for your session, so run it again whenever you reconnect.

If a query later says relation "books" does not exist and you are sure you typed it right, you almost certainly reconnected and lost the search_path. Run the SET again.

A friendlier window, if you want one

psql is entirely sufficient for this course, and being comfortable in it is a genuinely useful skill. But if you would rather click than type, DBeaver and pgAdmin are both free and both work with everything here. Point them at database practice on localhost.

Four psql commands worth knowing

CommandWhat it does
\dtList the tables
\d booksDescribe the books table — its columns and their types
\xToggle expanded display; helpful when rows are too wide to read
\qQuit

This is one lesson of 54

SQL Foundations: Your First Queries, Explained Properly continues from here — 4 lessons are free to read like this one, and the rest come with the course. Enrolled readers also get an AI tutor that has read the lesson they are on.

See the full course

Every free lesson in this course