Learn to persist data from a Go service using the standard database/sql package against PostgreSQL. You will connect through a driver, run parameterised queries, scan rows into structs, manage transactions and the connection pool, and wrap it all behind a clean repository layer you can trust in production.
Watch the free preview
The database/sql package and drivers — free to watch, no account needed.
What you'll learn
- Persist data from a Go service by connecting to PostgreSQL and running queries through database/sql
- Write parameterised queries that scan rows safely into Go values and structs
- Group multiple writes into a transaction with correct commit and rollback handling
- Tune the connection pool and apply context deadlines so the service stays reliable under load
- Structure data access behind a repository layer backed by migrated schema
Syllabus
Connecting a Go Service to PostgreSQL
The database/sql package and driversFree preview
Building a connection string and pool
Querying and Scanning Data
Parameterised queries and SQL injection
Scanning rows into Go values and structs
Transactions and Reliable Access
Transactions with commit and rollback
Context, timeouts, and pool tuning
Structuring Data Access
Migrations to evolve the schema
Building a repository layer
Lab — 10 Exercises & Solutions
Exercises 1–5
Exercises 6–10