PostgreSQL with pgx
Use a bounded pool and parameterized SQL.
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.
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.