Database Setup
REDB needs a set of tables, sequences and functions in your database. Pick the method that fits your workflow.
Project Template
Complete working project with models, CRUD, and tree queries — ready to run.
dotnet new install redb.Templatesdotnet new redb -n MyApp --db postgrescd MyApp && dotnet runParameters: --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.
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);Under the hood this calls EnsureDatabaseAsync(), which checks for the _schemes table and runs the full init script if it's missing.
// 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.
# 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" -vSee 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.
# 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.sqlredb schema -p postgres | psql -d mydb
redb schema -p mssql | sqlcmd -d mydbYou can also get the script from code:
var sql = redb.GetSchemaScript();
File.WriteAllText("redb_schema.sql", sql);D Docker / CI Pipeline
Run schema initialization as a container step or CI job.
# 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/workflows/deploy.yml
- name: Init REDB schema
run: |
dotnet tool install --global redb.CLI
redb init -p postgres -c "$REDB_CONNECTION" -vDocker Compose Examples
Ready-to-use compose files for running a database with REDB.
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: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 mssqlversion: '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:
# 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" -vComparison
| 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 | ✗ | ✓ | ✗ |