AI Hints
Home›PostgreSQL Tutorial›Project: Harbor Events, Part 1
PostgreSQL · Lesson 107 of 120

Project: Load and Check

Fill it, then test it.

⏱ 2 min read▶ Video at 1:37:28🎓 Free
Watch this lesson in the PostgreSQL courseWatch on YouTube ↗Full PostgreSQL course

Now some realistic data. Three venues, sixty events over four months, and about twelve hundred bookings, made with generate series and random.

  • 3 venues
  • 60 events
  • ~1,200 bookings

Load with generate_series

Generate series makes sixty events, two days apart, cycling through venues, titles and prices. Then each of forty guests books about half the events. Set seed makes the random numbers repeatable. Run it. Sixty events, and nearly twelve hundred bookings.

query.sqlsql
INSERT INTO venues (name, city, capacity) VALUES
  ('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
  ('Lighthouse Loft', 'Lunenburg', 60);

INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
       (ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
              'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
       timestamptz '2026-01-02 19:30' + g * interval '2 days',
       (ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;

SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
       timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;
Output
INSERT 0 3
INSERT 0 60
 setseed
---------

(1 row)

INSERT 0 1192
Also uses _setup.sql
CREATE TABLE venues (
  venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name     text NOT NULL UNIQUE,
  city     text NOT NULL,
  capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
  event_id  integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  venue_id  integer NOT NULL REFERENCES venues,
  title     text NOT NULL,
  starts_at timestamptz NOT NULL,
  price     numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
  booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  event_id   integer NOT NULL REFERENCES events,
  email      text NOT NULL,
  seats      integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
  booked_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (event_id, email));
Also uses _empty
1

Test the rules

A booking for twelve seats breaks the check rule. Booking the same event twice with one email breaks the unique rule. Run it. Both are refused, so bad data can't get in.

query.sqlsql
INSERT INTO bookings (event_id, email, seats) VALUES (1, 'ana@mail.com', 12);
INSERT INTO bookings (event_id, email, seats) VALUES (1, 'guest3@mail.com', 2);
Output
ERROR:  new row for relation "bookings" violates check constraint "bookings_seats_check"
DETAIL:  Failing row contains (1193, 1, ana@mail.com, 12, 2026-11-14
18:07:20.045906+00).
ERROR:  duplicate key value violates unique constraint "bookings_event_id_email_key"
DETAIL:  Key (event_id, email)=(1, guest3@mail.com) already exists.
Also uses _setup.sql
CREATE TABLE venues (
  venue_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  name     text NOT NULL UNIQUE,
  city     text NOT NULL,
  capacity integer NOT NULL CHECK (capacity > 0));
CREATE TABLE events (
  event_id  integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  venue_id  integer NOT NULL REFERENCES venues,
  title     text NOT NULL,
  starts_at timestamptz NOT NULL,
  price     numeric(6,2) NOT NULL CHECK (price >= 0));
CREATE TABLE bookings (
  booking_id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  event_id   integer NOT NULL REFERENCES events,
  email      text NOT NULL,
  seats      integer NOT NULL CHECK (seats BETWEEN 1 AND 10),
  booked_at  timestamptz NOT NULL DEFAULT now(),
  UNIQUE (event_id, email));
INSERT INTO venues (name, city, capacity) VALUES
  ('Harbor Hall', 'Halifax', 300), ('Pier Theatre', 'Halifax', 120),
  ('Lighthouse Loft', 'Lunenburg', 60);

INSERT INTO events (venue_id, title, starts_at, price)
SELECT 1 + g % 3,
       (ARRAY['Jazz Night', 'Comedy Hour', 'Folk Session', 'Poetry Slam',
              'Sea Shanty Evening', 'Indie Film Night'])[1 + g % 6],
       timestamptz '2026-01-02 19:30' + g * interval '2 days',
       (ARRAY[15, 20, 25, 30])[1 + g % 4]
FROM generate_series(1, 60) g;

SELECT setseed(0.7);
INSERT INTO bookings (event_id, email, seats, booked_at)
SELECT e, 'guest' || c || '@mail.com', 1 + (random() * 2)::int,
       timestamptz '2026-01-01' + random() * interval '100 days'
FROM generate_series(1, 60) e, generate_series(1, 40) c
WHERE random() < 0.5;
Also uses _empty
1

Summary

The data is loaded, and the rules refuse bad bookings. Next, the core queries.

Key takeaways
  • generate_series
  • setseed
  • Rules hold
Next up: Project: Core Queries — seats left and top guests