Database Setup

REDB needs a set of tables, sequences and functions in your database. Pick the method that fits your workflow.

Fastest

Project Template

Complete working project with models, CRUD, and tree queries — ready to run.

$dotnet new install redb.Templates
$dotnet new redb -n MyApp --db postgres
$cd MyApp && dotnet run

Parameters: --db postgres|mssql   --pro true|false

Database is created automatically on first run. Just set your connection string in Program.cs.

A Auto-Create from Code

The simplest approach. Pass ensureCreated: true to InitializeAsync and REDB will create all required objects if they don't exist.

Program.cs
var redb = services.GetRequiredService<IRedbService>();

// Creates schema if tables don't exist, then scans assemblies
await redb.InitializeAsync(ensureCreated: true);

// Or pass assemblies explicitly to register specific models
await redb.InitializeAsync(ensureCreated: true, typeof(Product).Assembly);
When to use: Local development, prototyping, small projects. The app itself manages the schema — no external tools needed.

Under the hood this calls EnsureDatabaseAsync(), which checks for the _schemes table and runs the full init script if it's missing.

Explicit two-step variant
// Step 1: Create schema
await redb.EnsureDatabaseAsync();

// Step 2: Register models (assembly is optional — omit to scan all referenced assemblies)
await redb.InitializeAsync();
// or specify assemblies explicitly:
// await redb.InitializeAsync(typeof(Product).Assembly);

B CLI Tool

Install the redb.CLI dotnet tool and initialize from your terminal.

bash
# Install once
dotnet tool install --global redb.CLI

# PostgreSQL
redb init -p postgres -c "Host=localhost;Database=mydb;Username=postgres;Password=pass" -v

# SQL Server
redb init -p mssql -c "Server=localhost;Database=mydb;User Id=sa;Password=pass;TrustServerCertificate=True" -v
When to use: Team onboarding, staging environments, when you want explicit control over when the schema is applied.

See CLI Reference for full command documentation including export and import.

C Manual SQL Script

Export the full initialization script, review it, and run it with your preferred SQL tool. Ideal for environments where a DBA must approve all schema changes.

bash
# Export to file
redb schema -p postgres -o redb_schema.sql

# Review
cat redb_schema.sql

# Apply with psql
psql -d mydb -f redb_schema.sql
Or pipe directly
redb schema -p postgres | psql -d mydb
redb schema -p mssql | sqlcmd -d mydb

You can also get the script from code:

csharp
var sql = redb.GetSchemaScript();
File.WriteAllText("redb_schema.sql", sql);
When to use: Production with strict change management, compliance requirements, or DBA-controlled deployments.

D Docker / CI Pipeline

Run schema initialization as a container step or CI job.

Dockerfile (multi-stage)
# Install CLI in build stage
FROM mcr.microsoft.com/dotnet/sdk:9.0 AS tools
RUN dotnet tool install --global redb.CLI
ENV PATH="$PATH:/root/.dotnet/tools"

# Init before app starts
ENTRYPOINT ["sh", "-c", "redb init -p postgres -c \"$REDB_CONN\" && dotnet MyApp.dll"]
GitHub Actions
# .github/workflows/deploy.yml
- name: Init REDB schema
  run: |
    dotnet tool install --global redb.CLI
    redb init -p postgres -c "$REDB_CONNECTION" -v
When to use: Automated deployments, container orchestration, ephemeral environments.

Docker Compose Examples

Ready-to-use compose files for running a database with REDB.

docker-compose.yml — PostgreSQL
version: '3.8'
services:
  pgdb:
    restart: always
    image: postgres:18
    container_name: pgdb
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: your_password
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
Dockerfile — SQL Server with Full-Text Search
FROM mcr.microsoft.com/mssql/server:2025-latest

USER root

# Install FTS package
RUN apt-get update && \
    apt-get install -y curl apt-transport-https gnupg && \
    curl -fsSL https://packages.microsoft.com/keys/microsoft.asc | apt-key add - && \
    curl -fsSL https://packages.microsoft.com/config/ubuntu/22.04/mssql-server-preview.list \
      -o /etc/apt/sources.list.d/mssql-server.list && \
    apt-get update && \
    ACCEPT_EULA=Y apt-get install -y mssql-server-fts && \
    apt-get clean && rm -rf /var/lib/apt/lists/*

USER mssql
docker-compose.yml — SQL Server
version: '3.8'
services:
  msdb:
    restart: always
    build: .  # uses the Dockerfile above
    container_name: msdb
    ports:
      - "1433:1433"
    environment:
      - MSSQL_SA_PASSWORD=YourStr0ngPassword!
      - ACCEPT_EULA=Y
    volumes:
      - mssql-data:/var/opt/mssql/data
      - mssql-log:/var/opt/mssql/log
      - mssql-secrets:/var/opt/mssql/secrets
    healthcheck:
      start_period: 1m
      interval: 1m30s
      timeout: 10s
      retries: 3

volumes:
  mssql-data:
  mssql-log:
  mssql-secrets:

After the container is running, init the REDB schema:

bash
# PostgreSQL
redb init -p postgres -c "Host=localhost;Database=mydb;Username=postgres;Password=your_password" -v

# SQL Server
redb init -p mssql -c "Server=localhost;Database=mydb;User Id=sa;Password=YourStr0ngPassword!;TrustServerCertificate=True" -v

Comparison

Method When DBA Review Automation Zero Config
A. Auto-Create Dev / Prototyping
B. CLI Staging / Team
C. SQL Script Production / Compliance
D. Docker / CI Containers / Pipelines