Skip to content

Getting Started ​

Install ​

console
dotnet add package MysticMind.PostgresEmbed

Start a server ​

csharp
await using var server = new PgServer("17");
await server.StartAsync();

// trust authentication: no password needed
await using var conn = new NpgsqlConnection(server.ConnectionString);
await conn.OpenAsync();

await using var cmd = new NpgsqlCommand("CREATE TABLE books(id int PRIMARY KEY, title text)", conn);
await cmd.ExecuteNonQueryAsync();
snippet source | anchor

The first start downloads and extracts the binaries, which takes a little while; later starts take about a second. Disposing the server stops it and, unless it is a named instance, deletes its data. You can also call StopAsync() yourself.

server.ConnectionString holds the host, port, user and database. The server listens on localhost only and uses trust authentication, so no password is needed and any password in a connection string is ignored.

Good to know ​

  • postgres is both the default database and the superuser.
  • The server gets a free port unless you set Port; read it from server.PgPort.
  • Each server has an instance folder with its data folder and its log (server.LogFile). Startup errors include the end of that log.
  • If the process exits without disposing the server, the library makes a best effort to kill the server, so tests never leave Postgres behind.
  • To see which platform and binaries the library uses on this machine:
csharp
PgPlatformInfo platform = PgPlatform.Detect();
Console.WriteLine($"{platform.Platform} {platform.Architecture}: {platform.ArtifactName}");
snippet source | anchor

Next ​

Released under the MIT License.