This repo uses SQLite so you can learn with zero setup. Most Go jobs use PostgreSQL. The good news: if you understand the SQL track, switching is mostly mechanical. This page lists what changes.
# docker-compose.yml
services:
db:
image: postgres:18
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: app
POSTGRES_DB: app
ports: ["5432:5432"]
volumes: ["pgdata:/var/lib/postgresql"]
volumes:
pgdata:docker compose up -d
docker compose exec db psql -U app # interactive SQL shell| SQLite (this repo) | PostgreSQL | |
|---|---|---|
| Driver import | _ "modernc.org/sqlite" |
_ "github.com/jackc/pgx/v5/stdlib" (database/sql) or github.com/jackc/pgx/v5/pgxpool (native, faster) |
sql.Open name |
"sqlite" |
"pgx" |
| DSN | file:app.db?_pragma=foreign_keys(1) |
postgres://app:app@localhost:5432/app?sslmode=disable |
| sqlc engine | engine: "sqlite" |
engine: "postgresql", sql_package: "pgx/v5" |
| goose dialect | goose.SetDialect("sqlite3") |
goose.SetDialect("postgres") |
Keep the DSN in an environment variable (DATABASE_URL) so code doesn't change between environments.
| Topic | SQLite | PostgreSQL |
|---|---|---|
| Placeholders | ? |
$1, $2, ... (sqlx Rebind converts; sqlc handles it) |
| Auto-increment id | INTEGER PRIMARY KEY |
BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY |
| UUID ids | TEXT |
UUID type (uuid.NewV7() from Go 1.27 stdlib works for both) |
| Timestamps | TEXT ISO-8601 |
TIMESTAMPTZ (scans straight into time.Time) |
| Booleans | INTEGER 0/1 |
BOOLEAN |
| Now | strftime('%Y-%m-%dT%H:%M:%SZ','now') |
now() |
| Enums | CHECK (status IN (...)) |
same CHECK, or CREATE TYPE ... AS ENUM |
| Get inserted id | RETURNING id (3.35+) or LastInsertId() |
RETURNING id (LastInsertId not supported) |
| Upsert | ON CONFLICT (...) DO UPDATE |
same |
| Foreign keys | must enable per connection | always on |
| Case-insensitive search | LIKE is case-insensitive for ASCII |
ILIKE |
| JSON | json_extract(col, '$.a') |
JSONB + col->>'a', GIN indexes |
| Concurrency | one writer at a time (WAL helps) | many concurrent writers (MVCC) |
| Isolation levels | serializable only | Read Committed default; Repeatable Read, Serializable |
This repo already uses RETURNING, ON CONFLICT, and CHECK constraints so most queries port unchanged.
EXPLAIN ANALYZEand reading query plans (seq scan vs index scan)- Index types: B-tree, GIN (JSONB, full-text, arrays), partial and expression indexes
SELECT ... FOR UPDATE/SKIP LOCKED— row locking, building job queues in PostgresLISTEN/NOTIFY, materialized views,COPYfor bulk loads (pgx.CopyFrom)- Connection pooling (
pgxpool, PgBouncer) andmax_connections - Integration tests with
testcontainers-go(real Postgres in Docker per test run)
- Add the compose file above; set
DATABASE_URL. - Change migration SQL types (id, timestamps, booleans) per the table.
- Change
sqlc.yamlengine topostgresql, regenerate withgo tool sqlc generate. - Replace driver import +
sql.Openname (or move topgxpool). - Map Postgres errors: unique violation is SQLSTATE
23505(*pgconn.PgError), FK violation23503. - Run tests against the container.