Enhanced.Testing.Component.PostgreSql 1.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Enhanced.Testing.Component.PostgreSql --version 1.5.0
                    
NuGet\Install-Package Enhanced.Testing.Component.PostgreSql -Version 1.5.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Enhanced.Testing.Component.PostgreSql" Version="1.5.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Enhanced.Testing.Component.PostgreSql" Version="1.5.0" />
                    
Directory.Packages.props
<PackageReference Include="Enhanced.Testing.Component.PostgreSql" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Enhanced.Testing.Component.PostgreSql --version 1.5.0
                    
#r "nuget: Enhanced.Testing.Component.PostgreSql, 1.5.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Enhanced.Testing.Component.PostgreSql@1.5.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Enhanced.Testing.Component.PostgreSql&version=1.5.0
                    
Install as a Cake Addin
#tool nuget:?package=Enhanced.Testing.Component.PostgreSql&version=1.5.0
                    
Install as a Cake Tool

Enhanced.Testing.Component.PostgreSql

NuGet

A PostgreSQL container harness for Enhanced.Testing.Component, with fast non-durable defaults and per-test template databases.

dotnet add package Enhanced.Testing.Component.PostgreSql

Usage

var postgreSql = new PostgreSqlHarness { ConnectionName = "Orders" };

var component = ComponentBuilder.Create<Program>()
                                .AddHarness(postgreSql)
                                .AddHarness(http)
                                .Build();

The application reads ConnectionStrings:Orders and gets the container's connection string. postgreSql.GetConnectionString() returns it to the test.

Option Default
Image postgres:15.1 The container image
Username, Password, Database Testcontainers defaults Credentials and database name
ConnectionProperties empty Extra connection string properties (escaped correctly)
Durable false See below

ConnectionName, ConfigureContainer and StartConcurrently are shared by all container harnesses; see Enhanced.Testing.Component.

To create the schema with EF Core and query the application's database, add Enhanced.Testing.Component.DbContext.

Fast, non-durable by default

Test data does not need to survive a crash, so the harness keeps the data directory (PGDATA) on tmpfs and runs with fsync, full_page_writes and synchronous_commit off. Nothing is written to disk, which reduces IO load when many containers run in parallel.

Set Durable = true to get the image's durable behaviour instead, for example when a test restarts the server and expects the data to survive, or when the database is too large to keep in memory:

var postgreSql = new PostgreSqlHarness { Durable = true };

The harness mounts tmpfs at /var/lib/postgresql/data; if ConfigureContainer mounts a volume or bind mount there, Docker rejects the duplicate mount point, so set Durable = true in that case.

Per-test databases from a template

To isolate tests that write to PostgreSQL without starting a container per test, prepare a template database once and copy it for each test. CREATE DATABASE ... TEMPLATE copies the database at the storage level, so a copy stays fast (about 60 ms, mostly docker exec overhead) even when building the schema and seed data takes much longer:

await postgreSql.CreateTemplateAsync("people_template", async connectionString =>
{
    var options = new DbContextOptionsBuilder<PeopleDbContext>().UseNpgsql(connectionString).Options;
    await using var context = new PeopleDbContext(options);
    await context.Database.EnsureCreatedAsync();
});

var connectionString = await postgreSql.CreateDatabaseAsync($"people_{Guid.NewGuid():N}", "people_template");
  • CreateTemplateAsync creates the database (dropping an existing one with that name), runs your callback with a non-pooled connection string, then marks the database as a template, blocks new connections to it and closes the open ones. PostgreSQL refuses to copy a template that has open connections.
  • CreateDatabaseAsync returns the new database's connection string. Without a template it creates an empty database.
  • DropDatabaseAsync drops a database (or a template) and closes its connections. GetConnectionString(database) returns the connection string for any database in the container.

The SQL runs through psql inside the container, so the package doesn't depend on Npgsql. Requires PostgreSQL 13 or later. Names are quoted, must be at most 63 bytes (PostgreSQL's identifier limit) and are case-sensitive.

Databases live as long as the container, so drop per-test databases with DropDatabaseAsync when a test finishes if the container is shared by many tests. CreateTemplateAsync drops and re-creates a template with the same name.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.6.0 81 11/22/2026
1.5.0 76 11/20/2026
1.4.0 171 11/13/2026
1.3.0 147 11/12/2026
1.2.4 2,054 3/4/2026
1.2.3 431 10/18/2024
1.2.2 269 8/7/2024
1.2.1 228 8/4/2024
1.2.0 235 8/1/2024
1.1.1 233 7/22/2024
1.1.0 266 7/21/2024
1.0.1 283 7/16/2024
1.0.0 263 6/14/2024