Skip to content

Helps on how to create an empty PostgreSQL database quickly  #26541

@JonPSmith

Description

@JonPSmith

I have added PostgreSQL helpers to my EfCore.TestSupport. These create unique database names for each xUnit test class, options with logging etc.

I have found that creating an empty database using EnsureDeleted, then EnsureCreated takes ~12 seconds on my setup ( EnsureDeleted = 4 seconds, EnsureCreated = 8 second), which I think is a bit slow (I want fast tests!).

I have therefore created a method that uses Jimmy Bogart's Respawn library, and that takes 2.2 seconds if there is a database already there.

I also tried the drop all tables approach. The PostgreSQL commands run quickly from pgAdmin but it actually takes longer than the Respawn approach (something like 4 seconds) because EnsureCreated has to add the tables.

My questions are:

  • Is 12 seconds for EnsureDeleted, then EnsureCreated a normal time, or is something I am doing wrong?
  • Do you know of a quicker way to create an empty a PostgreSQL database?

NOTE: you can see the code for the fast / respawn approach here.

Include provider and version information

NOTE: My PostgreSQL 12 server is running in a Windows Subsystem for Linux (WSL2). Its a fast PC.

EF Core version: 6.0.0-rc.2
Database provider: Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.0-rc.2"
Target framework: (e.g. .NET 6.0-rc.2)
Operating system: Windows
IDE: (e.g. Visual Studio 2022)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions