CURRICULUM PREVIEWRevision 1 · Open for reviewReview the full course
CONNECTING THE PIECES8 min read

PostgreSQL with pgx

Use a bounded pool and parameterized SQL.

Before you begingo-context

The idea

pgx provides PostgreSQL-native APIs and a connection pool. Parameter binding separates data from SQL syntax. Every booking lookup must also include the authorized tenant: parameterization prevents injection, while a tenant predicate prevents cross-tenant reads. Size the pool for the database’s actual connection budget.

Follow the flow

Make it concrete

This excerpt isolates the concept. Connect it to the booking API as you work through the exercise.

Focused example
Go
err := pool.QueryRow(ctx, `
    SELECT id, seats FROM bookings
    WHERE tenant_id = $1 AND id = $2`,
    tenantID, bookingID,
).Scan(&booking.ID, &booking.Seats)
if errors.Is(err, pgx.ErrNoRows) {
    return Booking{}, ErrNotFound
}

Decisions that matter

  • Set deadlines so exhausted pool waits cannot continue indefinitely.
  • Always close multi-row results and inspect rows.Err.
  • Use a dedicated migration identity and test least-privilege behavior.

Your turn

Why does parameterized SQL alone fail to prevent a user from requesting another tenant’s booking?

Reveal a worked answer

It protects query syntax, not object authorization. Include a trusted tenant scope in the predicate, then test the same booking ID with a different tenant. Keep the app role unable to change schema or grant permissions.

Keep exploring

One more piece connected.

Mark this lesson complete when you can explain the idea.