Getting Started
Install
console
dotnet add package MysticMind.PostgresEmbedStart a server
csharp
snippet source | anchorawait 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();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
postgresis both the default database and the superuser.- The server gets a free port unless you set
Port; read it fromserver.PgPort. - Each server has an instance folder with its
datafolder 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
snippet source | anchorPgPlatformInfo platform = PgPlatform.Detect();
Console.WriteLine($"{platform.Platform} {platform.Architecture}: {platform.ArtifactName}");