> ## Documentation Index
> Fetch the complete documentation index at: https://docs.scandrix.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Database Migrations & Pooling

> Configure PostgreSQL 16+, tune pgxpool connection limits, and execute automated database migrations.

ScanDrix requires **PostgreSQL 16+** with Row-Level Security (RLS) enabled. The Go backend uses `pgx/v5` with an internal pool manager (`pgxpool`) optimized for multi-tenant isolation and high-concurrency connection handling.

## Connection pool configuration

Tune PostgreSQL pool settings via environment variables in production:

| Variable | Default | Recommended Production | Description |
| - | - | - | - |
| `DATABASE_URL` | — | `postgres://scandrix_runtime:pass@host:5432/scandrix?sslmode=require` | Least-privilege application PostgreSQL URI. |
| `MIGRATION_DATABASE_URL` | — | `postgres://admin:pass@host:5432/scandrix?sslmode=require` | Privileged migration DSN (required for `CREATE EXTENSION`). |
| `DATABASE_MAX_CONNS` | `25` | `50` - `100` | Maximum open connections per API replica (also reads `DB_MAX_CONNS`). |
| `DATABASE_MIN_CONNS` | `2` | `10` | Minimum idle connections kept warm. |
| `DATABASE_ACQUIRE_TIMEOUT` | `8s` | `8s` - `15s` | Maximum wait when acquiring a pool connection. |

```go theme={null}
// Pool tuning in internal/database/db.go (DefaultPoolOptions):
// Env vars: DATABASE_MAX_CONNS (default 25), DATABASE_MIN_CONNS (default 2), DATABASE_ACQUIRE_TIMEOUT (default 8s)
// Simple protocol / PgBouncer-compatible pooler settings are configured per DSN.
```

***

## Running database migrations

ScanDrix ships a dedicated idempotent SQL migration binary (`cmd/migrate` / `scandrix-migrate`) that runs versioned migrations from `migrations/`:

```bash theme={null}
# Run migrations against MIGRATION_DATABASE_URL (recommended for CREATE EXTENSION)
MIGRATION_DATABASE_URL="postgres://admin:pass@host:5432/scandrix?sslmode=require" \
  go run ./cmd/migrate

# Or using the built binary / Docker Compose service
./bin/scandrix-migrate
docker compose run --rm migrate
```

### Standalone Docker migration runner

In automated CI/CD pipelines, run migrations as a pre-deploy Kubernetes Job:

```yaml theme={null}
apiVersion: batch/v1
kind: Job
metadata:
  name: scandrix-migration-job
spec:
  template:
    spec:
      restartPolicy: Never
      containers:
        - name: migrator
          image: ghcr.io/santosh-prasad-verma/scandrix/server:latest
          command: ["/app/bin/scandrix-migrate"]
          envFrom:
            - secretRef:
                name: scandrix-db-credentials
```

***

## Row-Level Security (RLS) verification

ScanDrix enforces tenant isolation at the database engine level. Every tenant-scoped query runs inside a transaction that sets:

```sql theme={null}
SET LOCAL app.current_tenant_id = 'org_abc123';
```

Any attempt by a query to read or write rows belonging to another organization ID is rejected by PostgreSQL with an RLS policy violation error.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.