Skip to main content

Database Setup

Configure and connect your Tracks application to a database.

Supported Drivers

DriverBest ForCGO Required
postgresProduction workloads, teamsNo
sqlite3Development, single-serverYes
go-libsqlEdge deployment with TursoYes
tracks new myapp --db postgres
tracks new myapp --db sqlite3
tracks new myapp --db go-libsql

Configuration

Database settings use environment variables with the APP_ prefix.

VariableTypeDefaultDescription
APP_DATABASE_URLstringvariesConnection string
APP_DATABASE_CONNECT_TIMEOUTduration10sConnection timeout
APP_DATABASE_MAX_OPEN_CONNSint25Max open connections (PostgreSQL)
APP_DATABASE_MAX_IDLE_CONNSint5Max idle connections (PostgreSQL)
APP_DATABASE_CONN_MAX_IDLE_TIMEduration5mMax idle time
APP_DATABASE_CONN_MAX_LIFETIMEduration1hMax lifetime

Connection Strings

PostgreSQL:

APP_DATABASE_URL=postgres://user:password@localhost:5432/myapp?sslmode=disable

SQLite3:

APP_DATABASE_URL=file:./data/myapp.db  # File-based
APP_DATABASE_URL=:memory: # In-memory

go-libsql (Turso):

APP_DATABASE_URL=http://localhost:8081                        # Local
APP_DATABASE_URL=libsql://your-db.turso.io?authToken=token # Cloud

Connection Pooling

PostgreSQL

Configurable pool settings for concurrent access:

db.SetMaxOpenConns(cfg.MaxOpenConns)       // default: 25
db.SetMaxIdleConns(cfg.MaxIdleConns) // default: 5
db.SetConnMaxIdleTime(cfg.ConnMaxIdleTime) // default: 5m
db.SetConnMaxLifetime(cfg.ConnMaxLifetime) // default: 1h

SQLite3

Single connection with WAL mode (enabled automatically):

db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)

go-libsql

Connects via HTTP to Turso—connection pooling is managed server-side. Local instances use the same single-connection pattern as SQLite3 but without WAL mode (Turso handles durability).

Local Development

PostgreSQL:

docker compose up -d postgres
make migrate-up
make dev

SQLite3:

mkdir -p data
make migrate-up
make dev

Production

PostgreSQL

  • Use managed services (AWS RDS, Cloud SQL)
  • Enable SSL: sslmode=require
  • Consider PgBouncer for high traffic

SQLite3

  • Use persistent volumes
  • Enable Litestream for backups
  • Single instance only

Health Checks

The /health endpoint verifies database connectivity:

{"status": "ok", "database": "ok", "timestamp": "2024-01-15T10:30:00Z"}