Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: Performance & internals
Stage 5: Specialization

Part 01 / Topic 9 of 10

Loading shopdb

Time to load the course's practice database: shopdb, a small online store. Every example from here to the end of Stage 4 runs against it.

Nine tables, one business: customers place orders for products; products belong to categories and come from suppliers; order_items are the lines of each order; payments settle orders; employees report to managers; events records what customers do on the site.

Two steps. First create an empty database (from your shell, not inside psql — createdb is a small helper PostgreSQL installs next to psql):

$ createdb shopdb

(Equivalently, inside psql: CREATE DATABASE shopdb; then \q.)

Second, download shopdb.sql and run it into the new database:

$ psql -d shopdb -f shopdb.sql

A stream of CREATE TABLE and INSERT messages scrolls past. That's the script building all nine tables and filling them. The script starts by dropping any old copies of the tables, so re-run it any time your data drifts from what a lesson shows.

Now connect and look around. \dt lists the tables:

\dt
            List of relations
 Schema |    Name     | Type  |  Owner   
--------+-------------+-------+----------
 public | categories  | table | postgres
 public | customers   | table | postgres
 public | employees   | table | postgres
 public | events      | table | postgres
 public | order_items | table | postgres
 public | orders      | table | postgres
 public | payments    | table | postgres
 public | products    | table | postgres
 public | suppliers   | table | postgres
(9 rows)

\d describes one table — columns, types, and the rules attached to them (those rules are constraints; Stage 3 is where you learn to write them):

\d customers
                          Table "public.customers"
   Column    |  Type   | Collation | Nullable |           Default            
-------------+---------+-----------+----------+------------------------------
 customer_id | integer |           | not null | generated always as identity
 first_name  | text    |           | not null | 
 last_name   | text    |           | not null | 
 email       | text    |           | not null | 
 city        | text    |           |          | 
 signup_date | date    |           | not null | 
Indexes:
    "customers_pkey" PRIMARY KEY, btree (customer_id)
    "customers_email_key" UNIQUE CONSTRAINT, btree (email)
Referenced by:
    TABLE "events" CONSTRAINT "events_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
    TABLE "orders" CONSTRAINT "orders_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customers(customer_id)

And the data is really there:

SELECT count(*) AS customers FROM customers;
 customers 
-----------
         6
(1 row)

That FROM customers is the piece your first queries were missing — Part 2 is entirely about it.

🧪 Quick quiz — answer from memory, then check.

  1. Which psql command describes the columns of the orders table?
  2. Your shopdb data got mangled while experimenting. How do you fix it?
Show the answers
  1. \d orders — \dt lists all tables, \l lists databases.
  2. Re-run the script: psql -d shopdb -f shopdb.sql. It rebuilds all nine tables from scratch.

Done here? The practice set for Part 1 — 6 exercises with solutions — is on the part page.

Search the guide