Npgsql.DependencyInjection 9.0.3

Npgsql is the open source .NET data provider for PostgreSQL. It allows you to connect and interact with PostgreSQL server using .NET.

This package helps set up Npgsql in applications using dependency injection, notably ASP.NET applications. It allows easy configuration of your Npgsql connections and registers the appropriate services in your DI container.

For example, if using the ASP.NET minimal web API, simply use the following to register Npgsql:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNpgsqlDataSource("Host=pg_server;Username=test;Password=test;Database=test");

This registers a transient NpgsqlConnection which can get injected into your controllers:

app.MapGet("/", async (NpgsqlConnection connection) =>
{
    await connection.OpenAsync();
    await using var command = new NpgsqlCommand("SELECT number FROM data LIMIT 1", connection);
    return "Hello World: " + await command.ExecuteScalarAsync();
});

But wait! If all you want is to execute some simple SQL, just use the singleton NpgsqlDataSource to execute a command directly:

app.MapGet("/", async (NpgsqlDataSource dataSource) =>
{
    await using var command = dataSource.CreateCommand("SELECT number FROM data LIMIT 1");
    return "Hello World: " + await command.ExecuteScalarAsync();
});

NpgsqlDataSource can also come in handy when you need more than one connection:

app.MapGet("/", async (NpgsqlDataSource dataSource) =>
{
    await using var connection1 = await dataSource.OpenConnectionAsync();
    await using var connection2 = await dataSource.OpenConnectionAsync();
    // Use the two connections...
});

The AddNpgsqlDataSource method also accepts a lambda parameter allowing you to configure aspects of Npgsql beyond the connection string, e.g. to configure UseLoggerFactory and UseNetTopologySuite:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNpgsqlDataSource(
    "Host=pg_server;Username=test;Password=test;Database=test",
    builder => builder
        .UseLoggerFactory(loggerFactory)
        .UseNetTopologySuite());

Finally, starting with Npgsql and .NET 8.0, you can now register multiple data sources (and connections), using a service key to distinguish between them:

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddNpgsqlDataSource("Host=localhost;Database=CustomersDB;Username=test;Password=test", serviceKey: DatabaseType.CustomerDb)
    .AddNpgsqlDataSource("Host=localhost;Database=OrdersDB;Username=test;Password=test", serviceKey: DatabaseType.OrdersDb);

var app = builder.Build();

app.MapGet("/", async ([FromKeyedServices(DatabaseType.OrdersDb)] NpgsqlConnection connection)
    => connection.ConnectionString);

app.Run();

enum DatabaseType
{
    CustomerDb,
    OrdersDb
}

For more information, see the Npgsql documentation.

No packages depend on Npgsql.DependencyInjection.

Version Downloads Last updated
9.0.3 3 04/11/2025
9.0.2 0 12/07/2024
9.0.1 0 11/19/2024
9.0.0 0 11/18/2024
8.0.7 0 02/24/2025
8.0.6 0 11/18/2024
8.0.5 0 10/13/2024
8.0.4 0 09/10/2024
8.0.3 0 05/09/2024
8.0.2 0 02/10/2024
8.0.1 0 12/04/2023
8.0.0 0 11/21/2023
8.0.0-rtm 0 11/20/2023
8.0.0-rc.2 0 10/11/2023
8.0.0-preview.4 0 05/17/2023
8.0.0-preview.3 0 04/24/2023
8.0.0-preview.2 0 03/20/2023
8.0.0-preview.1 0 03/03/2023
7.0.10 0 03/17/2025
7.0.9 0 11/18/2024
7.0.8 0 09/10/2024
7.0.7 0 05/09/2024
7.0.6 0 09/14/2023
7.0.4 0 04/24/2023
7.0.2 0 02/15/2023
7.0.1 0 12/17/2022
7.0.0 0 11/09/2022
7.0.0-rc.2 0 10/11/2022
7.0.0-rc.1 0 09/16/2022
7.0.0-preview.7 0 08/09/2022
7.0.0-preview.6 0 07/13/2022