Skip to content

Options ​

A server is configured with PgServerOptions:

csharp
await using var server = new PgServer(new PgServerOptions
{
    PgVersion = "17",
    DbDir = dbDir,                  // default: the current directory
    ServerParams = new Dictionary<string, string>
    {
        ["max_connections"] = "300",
        ["timezone"] = "UTC",
        ["synchronous_commit"] = "off"
    },
    StartupTimeout = TimeSpan.FromSeconds(60),
    Logger = loggerFactory.CreateLogger("Postgres")
});
await server.StartAsync();
Console.WriteLine(server.PgPort);   // a free port unless you set Port
snippet source | anchor
OptionDefaultDescription
PgVersionrequiredExact version (17.6.0) or a spec (17, 17.6, latest); see Versions
PgUserpostgresSuperuser created by initdb
Portfree portTCP port; read the actual one from server.PgPort
DbDircurrent directoryA pg_embed folder is created under it
BinariesDir<DbDir>/pg_embed/binaries, or binaries in the shared cache for named instances and when PGEMBED_CACHE_DIR is setCache of downloads and extracted installs
InstanceNamenoneKeep and share an instance under a name; see Named Instances
AttachIfRunningtrueAttach to the instance's server when it is already running
InstanceIdnew GuidOlder way to reuse an instance: prefer InstanceName (set one or the other)
ClearInstanceDirOnStoptrue for a new instance, false for a named oneDelete the instance folder when the server stops
ClearWorkingDirOnStartfalseDelete the pg_embed folder before starting
KeepRunningAfterExitfalseKeep the server running when the process exits without stopping it (by default it is killed, so tests never leave Postgres behind)
ServerParamsnonePostgres settings, written to a config file (values may contain spaces and quotes)
ExtensionsnoneExtension archives to install; see Extensions
Localeen_US.UTF-8 (system locale on Windows)Locale passed to initdb
AddLocalUserAccessPermissionfalseWindows only; see Troubleshooting
StartupTimeout / ShutdownTimeout30 s / 10 s
MavenRepoMaven CentralA mirror or an internal proxy
RequireChecksumfalseFail a download whose repository publishes no SHA-256, instead of installing it unverified
DownloadRetryCount3Retries for each download
DeleteFolderRetryCount / DeleteFolderInitialDelay5 / 16 msRetries when deleting folders that are briefly locked; the delay doubles each time
HttpClientshared clientConfigure proxies or custom handlers
LoggernoneAny Microsoft.Extensions.Logging logger
InstallProgressnoneIProgress<PgInstallProgress>: download bytes, checksum check and extraction; see Show progress
Platform / ArchitecturedetectedOverride detection

Environment ​

VariableEffect
PGEMBED_CACHE_DIRThe shared cache folder; also makes its binaries folder the default BinariesDir for every server
XDG_CACHE_HOMEOn Linux, the shared cache is $XDG_CACHE_HOME/pgembed when PGEMBED_CACHE_DIR is not set

Released under the MIT License.