How-to guide

Switch a template to Postgres

Source content/

The project templates record runs in Postgres when ConnectionStrings:Trax is set, and in memory when it is not. In memory loses runs on restart, shares nothing between processes and leaves mode: QUEUE refused with TRAX_QUEUE_UNAVAILABLE, so the templates allow it only in Development: started anywhere else without the connection string, they refuse to start and name it, unless Trax:AllowInMemory is true, which their own tests set.

  1. Start a Postgres:

    docker run -d --name trax-db -p 127.0.0.1:5432:5432 \
      -e POSTGRES_USER=trax -e POSTGRES_PASSWORD=trax123 -e POSTGRES_DB=trax postgres:17
  2. Put the connection string in appsettings.Development.json, or in the environment as ConnectionStrings__Trax:

    {
      "ConnectionStrings": {
        "Trax": "Host=localhost;Port=5432;Database=trax;Username=trax;Password=trax123"
      }
    }
  3. Run the host. Trax creates and migrates its trax schema at startup, and the dashboard's runs now survive a restart. Program.cs already chooses the provider from the setting:

    var traxDatabase = builder.Configuration.GetConnectionString("Trax");
    // ... refuses to start outside Development when it is null ...
    effects => (traxDatabase is null ? effects.UseInMemory() : effects.UsePostgres(traxDatabase))

A project made from a template before this choice was added has UseInMemory() alone: add the Trax.Effect.Data.Postgres package at the version of its other Trax.* packages and make the same change.

With a database, the scheduler queues work in it and runs it on local workers, so a [TraxMutation] called with mode: QUEUE runs, and several processes on one database share the work. HelloWorldTrain exposes Run only; change its [TraxMutation(GraphQLOperation.Run)] to [TraxMutation] to serve the queue mode too.

The schema changes each release applies are listed in Schema migrations.

Move the application context

In the trax-hub and trax-api templates the application context, AppDbContext, is separate and stays in memory. Its tables go under the schema in Data/AppSchema.cs, and on Postgres they need EF Core migrations, as the comment above EnsureCreated() in Program.cs says. EnsureCreated() creates nothing in a database that already has tables, and UsePostgres has created Trax's by the time it runs, so the host starts without error and the first query of AppDbContext fails with 42P01: relation "app.notes" does not exist.

  1. Add the EF Core packages and the dotnet ef tool:

    dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL
    dotnet add package Microsoft.EntityFrameworkCore.Design
    dotnet new tool-manifest
    dotnet tool install dotnet-ef
  2. Change UseInMemoryDatabase("app") to UseNpgsql, keeping EF Core's migration history in the application's schema:

    builder.Services.AddDbContextFactory<AppDbContext>(options =>
        options.UseNpgsql(
            builder.Configuration.GetConnectionString("TraxDatabase"),
            npgsql => npgsql.MigrationsHistoryTable("__EFMigrationsHistory", AppSchema.Name)
        )
    );

    Without MigrationsHistoryTable, the history table goes to the first schema on the connection's search_path, which is trax when the database user is named trax.

  3. Replace db.Database.EnsureCreated(); with db.Database.Migrate();.

  4. Create the first migration. dotnet ef runs Program.cs in the Development environment, so it reads ConnectionStrings:TraxDatabase from appsettings.Development.json:

    dotnet ef migrations add InitialApp --context AppDbContext

The host creates the app schema and its tables at startup, and applies each migration you add after a model change.

SDK Reference

UsePostgres | UseInMemory | TraxQuery / TraxMutation