PostgreSQL Production API
Build a Go HTTP service over Postgres the way it is actually operated: a real pool, timeouts, migrations and indexes — one small change at a time.
A toy SELECT 1 demo teaches nothing about the night Postgres falls over. Here we build a small Go service over Postgres and add, step by tiny step, the things that decide whether it survives load: a connection pool sized on purpose, statement and request timeouts, migrations applied on boot, and the right indexes. Each page is one delta with a short note on why it matters — and what incident it prevents.
What you'll build
- Serve HTTP from Go with a Postgres-backed handler
- Manage a connection pool with sane limits
- Apply SQL migrations on startup
- Protect the service with statement and request timeouts
- Index the columns your queries actually filter on
Contents
- An empty Go module
- A database to talk to
- The smallest HTTP surface
- Make it serve
- Reach for a pool, not a connection
- Connect at boot, fail loud
- A health check that tells the truth
- Schema as a migration
- A tiny migration runner
- Migrate before serving
- Give the database a typed home
- Insert an order
- Read an order back
- Expose order creation
- And reading one back
- Wire the routes together
- Size the pool on purpose
- Stop runaway queries
- Index what you filter on
- Timeouts on the HTTP side too