Setting up PostgreSQL
Overview
To write SQL you need two things: a database server that stores the data and runs queries, and a client that sends your SQL to it and shows the results. In this lesson you’ll set up both, load the Bean & Leaf database, and learn the handful of psql commands you’ll use every day.
Pick whichever installation route suits you — they all end in the same place.
Option 1: install PostgreSQL on your computer
- Windows: download the installer from postgresql.org/download. It installs the server,
psqland pgAdmin. Remember the password you choose for thepostgresuser. - macOS: the simplest options are Postgres.app (drag to Applications, click Initialize) or Homebrew:
brew install postgresql@19, thenbrew services start postgresql@19. - Linux: use the packages from postgresql.org’s download page for your distribution, for example
sudo apt install postgresql-19on Debian or Ubuntu after adding the PostgreSQL apt repository.
Check it worked by asking psql for its version:
psql --version
# psql (PostgreSQL) 19.0 (or a later 19.x)Option 2: run it in Docker
If you use Docker, one command starts a disposable server:
# in a terminal:
docker run --name shop-db -e POSTGRES_PASSWORD=secret -p 5432:5432 -d postgres:19
docker exec -it shop-db psql -U postgresThe container keeps running in the background; docker stop shop-db and docker start shop-db stop and restart it.
Option 3: a free cloud database
Services such as Neon, Supabase and Aiven offer free PostgreSQL databases in the browser — no installation at all. Create a project, copy the connection string they give you, and paste it into psql or a graphical client. This is also how many real applications connect to their production database.
Connecting: the connection string
Whatever route you took, a connection needs five pieces of information:
| Piece | Example | Default |
|---|---|---|
| host | localhost or db.example.com |
localhost |
| port | 5432 |
5432 |
| user | postgres |
your OS user name |
| password | secret |
— |
| database | shop |
same as the user name |
They’re often combined into a single connection URL: postgresql://postgres:secret@localhost:5432/shop. You can pass that straight to psql: psql "postgresql://postgres:secret@localhost:5432/shop", or give the pieces separately with psql -h localhost -p 5432 -U postgres -d shop.
Security: a connection string with a password in it is a secret. Don’t paste production ones into chat tools or commit them to Git.
Load the Bean & Leaf database
Download shop.sql. It’s a plain text file of SQL: it creates the six tables and inserts every row. Open it in a text editor if you’re curious — by the end of the course you’ll understand every line.
Create an empty database called shop, then run the file into it:
# in a terminal, from the folder containing shop.sql:
createdb -U postgres shop
psql -U postgres -d shop -f shop.sqlIf you’re already inside psql, the equivalent is:
CREATE DATABASE shop;
\c shop
\i shop.sql\c connects to another database and \i runs a file of SQL. The script starts by dropping the tables if they exist, so you can run it again at any time to reset the data — handy after the lessons where you change rows.
Finding your way around with psql
psql understands ordinary SQL, plus its own backslash commands for exploring. Backslash commands are a psql feature, not SQL: they don’t end with a semicolon and won’t work in other tools. The essentials:
| Command | What it does |
|---|---|
\l |
list databases |
\c shop |
connect to the shop database |
\dt |
list tables |
\d products |
describe one table: columns, types, keys |
\x |
toggle expanded display (one column per line — great for wide rows) |
\i file.sql |
run a file |
\? |
help on backslash commands |
\h SELECT |
help on an SQL command |
\q |
quit |
List the tables:
\dt List of relations
Schema | Name | Type | Owner
--------+-------------+-------+----------
public | categories | table | postgres
public | customers | table | postgres
public | employees | table | postgres
public | order_items | table | postgres
public | orders | table | postgres
public | products | table | postgres
(6 rows)And describe one:
\d categories Table "public.categories"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+----------------------------------
id | integer | | not null | generated by default as identity
name | text | | not null |
Indexes:
"categories_pkey" PRIMARY KEY, btree (id)
"categories_name_key" UNIQUE CONSTRAINT, btree (name)
Referenced by:
TABLE "products" CONSTRAINT "products_category_id_fkey" FOREIGN KEY (category_id) REFERENCES categories(id)That one screen tells you a lot: the column types, that id is generated automatically, that name must be unique, and — under Referenced by — that products.category_id is a foreign key pointing here. (Depending on your psql version, \d may also list the not-null constraints by name, and \d+ shows even more detail.)
Now run some SQL. A query can span several lines; psql waits until it sees the semicolon:
SELECT name, price, stock
FROM products
WHERE stock = 0; name | price | stock
-------------------+-------+-------
Decaf House Blend | 11.25 | 0
Glass Teapot | 32.00 | 0
(2 rows)Two products are out of stock. If psql’s prompt changes from shop=# to shop-#, it’s waiting for the rest of a statement — usually a forgotten semicolon. Type ; and press Enter.
Graphical clients
If you prefer a window to a terminal, connect with the same details from:
- pgAdmin — the official PostgreSQL tool, included with the Windows and macOS installers.
- DBeaver — free, works with almost every database.
- DataGrip — JetBrains’ paid database IDE, with excellent autocomplete.
- VS Code — the official PostgreSQL extension or SQLTools.
Every one of them has a query editor where you type SQL and a results grid. The lessons use psql’s text output because it’s precise and easy to compare, but any client works for the exercises.
psql tips that save time
- Press ↑ to recall earlier commands, and Tab to complete table and column names.
\timing onshows how long each query took.\pset null '(null)'makes NULL values visible instead of blank.- A long result opens in a pager; press q to get back to the prompt, or run
\pset pager off.
Tip: keep
shop.sqlsomewhere easy to find. Whenever an experiment goes wrong — you deleted the wrong rows, or dropped a table — just run it again and you’re back to a clean shop in under a second.
Check your copy of the shop
Once you've loaded shop.sql, make sure everything arrived. Write one query per table that counts its rows, using SELECT count(*) FROM table_name; — for categories, products, customers, orders, order_items and employees, in that order. Your counts should be 4, 12, 10, 24, 42 and 7. If any differ, run the file again: it drops and recreates the tables each time.
SELECT count(*) FROM categories;
-- one query for each of the other five tablesSELECT count(*) FROM categories;
SELECT count(*) FROM products;
SELECT count(*) FROM customers;
SELECT count(*) FROM orders;
SELECT count(*) FROM order_items;
SELECT count(*) FROM employees;Summary
- PostgreSQL is a server: you install it (or run it in Docker or the cloud) and connect to it with a client.
- psql is the standard command-line client; pgAdmin, DBeaver, DataGrip and VS Code extensions are popular graphical clients.
- A connection needs a host, port (5432 by default), user, password and database name — often written as one connection URL.
- Load the course database with psql -d shop -f shop.sql; it can be re-run at any time to reset the data.
- psql backslash commands such as \dt, \d table, \x and \q explore the database; they are psql features, not SQL.