PostgreSQL Quick Start

This tutorial will get you from zero to up and running with SQL and MassTransit.

Walkthrough Video TBD

  • The source for this sample is available on GitHub.

Prerequisites

The following instructions assume you are starting from a completed In-Memory Quick Start

This example requires the following:

  • a functioning installation of the dotnet runtime and sdk (at least 6.0)
  • a functioning installation of Docker with Docker Compose support enabled.

Run PostgreSQL

For this quick start, we recommend running the preconfigured official Docker image of Postgres.

$ docker run -p 5432:5432 postgres

If you are running on an ARM platform

$ docker run --platform linux/arm64 -p 5432:5432 postgres

Once its up and running you can use your preferred tool to browse into the database.

Configure PostgreSQL

Add the MassTransit.SqlTransport.PostgreSQL package to the project.

$ dotnet add package MassTransit.SqlTransport.PostgreSQL

Edit Program.cs

Change UsingInMemory to UsingPostgres as shown below.

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureServices((hostContext, services) =>
        {
            services.AddOptions<SqlTransportOptions>().Configure(options =>
            {
                  options.Host = "localhost";
                  options.Database = "sample";
                  options.Schema = "transport";
                  options.Role = "transport";
                  options.Username = "masstransit";
                  options.Password = "H4rd2Gu3ss!";
                  
                  // credentials to run migrations
                  options.AdminUsername = "migration-user";
                  options.AdminPassword = "H4rderTooGu3ss!!;
            });
            // MassTransit will run the migrations on start up
            services.AddPostgresMigrationHostedService();
            services.AddMassTransit(x =>
            {
                // elided...

                x.UsingPostgres((context,cfg) =>
                {
                    cfg.ConfigureEndpoints(context);
                });
            });

            services.AddHostedService<Worker>();
        });
SettingDescription
HostThe host to connect to. We are using localhost to connect to the docker container
PortWe are using the default 5432, so we aren't setting it.
DatabaseThe name of the database to connect to
SchemaThe schema to place the tables and functions inside of
Rolethe role to assign for all created tables, functions, etc.
UsernameThe username of the user to login as for normal operations
PasswordThe password of the user to login as for normal operations
AdminUsernameThe username of the admin user to login as when running migration commands
AdminPasswordThe password of the admin user to login as when running migration commands

Run the Project

$ dotnet run

The output should have changed to show the message consumer generating the output (again, press Control+C to exit). Notice that the bus address now starts with db.

Building...
info: MassTransit[0]
      Configured endpoint Message, Consumer: GettingStarted.MessageConsumer
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /Users/chris/Garbage/start/GettingStarted
info: MassTransit[0]
      Bus started: db://localhost/
info: GettingStarted.MessageConsumer[0]
      Received Text: The time is 3/24/2021 12:11:10 PM -05:00

At this point the service is connecting to PostgreSQL on localhost and publishing messages which are received by the consumer.

๐ŸŽ‰