Book 4 · Lesson 4.6

Postgres schema and SQLx

  • Connect DATABASE_URL to compose Postgres
  • Review team_events DDL
  • Use ON CONFLICT for idempotent inserts

Prerequisites: 4.5 · 2.5

Analyzer requires Postgres at startup — pool fails fast if DB is down.

PgPoolOptions::new()
    .max_connections(5)
    .connect(&database_url)
    .await?;

PostgresSchemaDiagraminfra/docker/init-db.sql.

Idempotent write

INSERT INTO team_events (…) VALUES (…)
ON CONFLICT (id) DO UPDATE SET

Enables safe Kafka replay without duplicate primary keys.

Teach-back prompt

Why exit the process if Postgres is unavailable?

SQLx pool connect

Lesson 4.6 check

1. ON CONFLICT on team_events enables…