The Go Template includes built-in PostgreSQL support with migrations (goose) and type-safe query generation (sqlc).Documentation Index
Fetch the complete documentation index at: https://mintlify.com/aarock1234/go-template/llms.txt
Use this file to discover all available pages before exploring further.
Database Stack
The template uses:- PostgreSQL 18 - Alpine-based container for local development
- goose - Migration tool for versioning schema changes
- sqlc - Generates type-safe Go code from SQL queries
- pgx/v5 - High-performance PostgreSQL driver and toolkit
PostgreSQL Setup
Starting the Database
The PostgreSQL service is opt-in. You can use the bundled Docker container or connect to an external database.- Docker (Local)
- External Database
Start the PostgreSQL container:This starts PostgreSQL on
localhost:5432 with:- User:
postgres - Password:
postgres - Database:
app
Database Configuration
Configure your database connection in.env:
Database Migrations
The template uses goose for managing database schema changes.Migration Files
Migrations are stored inpkg/db/migrations/. Each migration is a SQL file with up and down sections:
Creating Migrations
Migration Commands
| Command | Description |
|---|---|
make migrate | Run all pending migrations |
make migrate-down | Roll back the last migration |
make migrate-new | Create a new migration file |
Type-Safe Queries with sqlc
The template uses sqlc to generate type-safe Go code from SQL queries.Configuration
The sqlc configuration is inpkg/db/sqlc.yaml:
- queries:
./queries- SQL query files - schema:
./migrations- Migration files (for schema awareness) - out:
./sqlc- Generated Go code output directory - sql_package:
pgx/v5- Uses pgx driver
Writing Queries
Query Annotations
sqlc uses special comments to generate code::one- Returns a single row (error if not found):many- Returns a slice of rows:exec- Executes without returning rows:execrows- Returns number of affected rows
Database Package
Thepkg/db package wraps pgxpool with additional features.
Connection Pool
Transactions
The package provides two ways to work with transactions:- InTx (Recommended)
- Begin (Manual)
Automatic transaction management with rollback on error:
Advisory Locks
For distributed coordination, the package supports PostgreSQL advisory locks:Null Handling
The template uses null/v9 for nullable database fields:Code Generation
The database package includes a go:generate directive:Best Practices
Always use transactions for multi-step operations
Always use transactions for multi-step operations
Use
InTx to ensure atomicity:Test both up and down migrations
Test both up and down migrations
Ensure migrations can be rolled back:
Use descriptive migration names
Use descriptive migration names
Good:
create_users_tableadd_email_index_to_usersadd_status_column_to_orders
migration1updatefix
Keep queries in separate files by domain
Keep queries in separate files by domain
Organize queries by feature:
queries/users.sqlqueries/orders.sqlqueries/products.sql
Next Steps
Testing
Learn about testing database code
Local Setup
Set up your development environment
