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.
Showing the top 20 packages that depend on Npgsql.DependencyInjection.
Packages | Downloads |
---|---|
Aspire.Npgsql
A PostgreSQL® client that integrates with Aspire, including health checks, metrics, logging, and telemetry.
|
3 |
Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
A PostgreSQL® provider for Entity Framework Core that integrates with Aspire, including connection pooling, health checks, logging, and telemetry.
|
3 |
Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
A PostgreSQL® provider for Entity Framework Core that integrates with Aspire, including connection pooling, health checks, logging, and telemetry.
|
2 |
Aspire.Npgsql
A PostgreSQL® client that integrates with Aspire, including health checks, metrics, logging, and telemetry.
|
2 |
.NET 6.0
- Npgsql (>= 9.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
.NET 8.0
- Npgsql (>= 9.0.3)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
Version | Downloads | Last updated |
---|---|---|
9.0.3 | 1 | 6/14/2025 |
9.0.2 | 1 | 6/14/2025 |
9.0.1 | 1 | 6/14/2025 |
9.0.0 | 1 | 6/12/2025 |
8.0.7 | 1 | 6/14/2025 |
8.0.6 | 2 | 6/12/2025 |
8.0.5 | 1 | 6/13/2025 |
8.0.4 | 1 | 6/14/2025 |
8.0.3 | 1 | 6/13/2025 |
8.0.2 | 1 | 6/14/2025 |
8.0.1 | 1 | 6/13/2025 |
8.0.0 | 1 | 6/12/2025 |
8.0.0-rtm | 1 | 6/14/2025 |
8.0.0-rc.2 | 1 | 6/14/2025 |
8.0.0-preview.4 | 1 | 6/14/2025 |
8.0.0-preview.3 | 2 | 6/11/2025 |
8.0.0-preview.2 | 1 | 6/14/2025 |
8.0.0-preview.1 | 1 | 6/13/2025 |
7.0.10 | 1 | 6/14/2025 |
7.0.9 | 1 | 6/14/2025 |
7.0.8 | 1 | 6/14/2025 |
7.0.7 | 1 | 6/13/2025 |
7.0.6 | 1 | 6/14/2025 |
7.0.4 | 1 | 6/13/2025 |
7.0.2 | 1 | 6/14/2025 |
7.0.1 | 1 | 6/14/2025 |
7.0.0 | 1 | 6/13/2025 |
7.0.0-rc.2 | 1 | 6/13/2025 |
7.0.0-rc.1 | 1 | 6/13/2025 |
7.0.0-preview.7 | 1 | 6/13/2025 |
7.0.0-preview.6 | 1 | 6/14/2025 |