Skip to content

Database Configuration

SLOzy uses PostgreSQL 16+ as its primary data store and Redis 7+ for caching and ephemeral state.

Connection String

The backend builds a libpq-style connection string from individual env vars in internal/config/config.go:

go
func (c *Config) GetPostgresConnectionString() string {
    return "host=" + c.PostgresHost +
        " port=" + strconv.Itoa(c.PostgresPort) +
        " dbname=" + c.PostgresDB +
        " user=" + c.PostgresUser +
        " password=" + c.PostgresPassword +
        " sslmode=" + c.PostgresSSLMode
}

Alternatively, the .env.production file supports a DATABASE_URL connection string format:

DATABASE_URL=postgres://postgres:postgres@postgres-service:5432/slozy_db?sslmode=require

Choose one approach — they are mutually exclusive in the current codebase.

Connection Pool

Fine-tune the pool based on your workload and available database connections.

VariableDefaultSuggestion
POSTGRES_MAX_OPEN_CONNS2550-100 for production with moderate load
POSTGRES_MAX_IDLE_CONNS1010-25 — enough to handle bursts
POSTGRES_CONN_MAX_LIFETIME1hKeep at 1h to avoid stale connections
POSTGRES_CONN_MAX_IDLETIME10m5-10m balances reuse and freshness

SSL Mode

  • Development: disable
  • Production: require or verify-full
  • When using verify-full, provide the CA certificate path via the PGSSLCERT / PGSSLKEY env vars (or bake it into the Docker image).

Migrations

Migration SQL files live in migrations/ and follow the pattern {version}_{name}.{up|down}.sql:

migrations/
  000001_organizations.up.sql
  000001_organizations.down.sql
  000002_teams.up.sql
  000002_teams.down.sql
  000003_users.up.sql
  000003_users.down.sql

Run migrations through the Makefile or your CI pipeline:

bash
make migrate-up
make migrate-down

In Docker Compose, migrations can be applied at container start by mounting the migrations/ directory to /docker-entrypoint-initdb.d.

Local Development

Use the docker-compose.yml file to spin up PostgreSQL and Redis:

bash
docker-compose up -d postgres redis

Default credentials (postgres / postgres) are set in the compose file. Point your .env values to localhost:5432.

Redis

Redis stores:

  • Session cache: JWT blacklist and user sessions (TTL: 8h)
  • SLO status cache: Recent SLO burn-rate data (TTL: 60s)
  • Prometheus query cache: Aggregated metric query results (TTL: 30s)

Redis is optional — the application degrades gracefully if Redis is unreachable (cache is bypassed).