![]() |
VOOZH | about |
dotnet add package Npgsql.DependencyInjection --version 10.0.3
NuGet\Install-Package Npgsql.DependencyInjection -Version 10.0.3
<PackageReference Include="Npgsql.DependencyInjection" Version="10.0.3" />
<PackageVersion Include="Npgsql.DependencyInjection" Version="10.0.3" />Directory.Packages.props
<PackageReference Include="Npgsql.DependencyInjection" />Project file
paket add Npgsql.DependencyInjection --version 10.0.3
#r "nuget: Npgsql.DependencyInjection, 10.0.3"
#:package Npgsql.DependencyInjection@10.0.3
#addin nuget:?package=Npgsql.DependencyInjection&version=10.0.3Install as a Cake Addin
#tool nuget:?package=Npgsql.DependencyInjection&version=10.0.3Install as a Cake Tool
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.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
Showing the top 5 NuGet packages that depend on Npgsql.DependencyInjection:
| Package | Downloads |
|---|---|
|
Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
A PostgreSQL® provider for Entity Framework Core that integrates with Aspire, including connection pooling, health checks, logging, and telemetry. |
|
|
Aspire.Npgsql
A PostgreSQL® client that integrates with Aspire, including health checks, metrics, logging, and telemetry. |
|
|
Aspire.Azure.Npgsql.EntityFrameworkCore.PostgreSQL
An Azure Database for PostgreSQL® provider for Entity Framework Core that integrates with Aspire, including connection pooling, health checks, logging, and telemetry. |
|
|
Aspire.Azure.Npgsql
A client for Azure Database for PostgreSQL® that integrates with Aspire, including health checks, logging and telemetry. |
|
|
Eventuous.Postgresql
Production-grade Event Sourcing library |
Showing the top 8 popular GitHub repositories that depend on Npgsql.DependencyInjection:
| Repository | Stars |
|---|---|
|
quartznet/quartznet
Quartz Enterprise Scheduler .NET
|
|
|
microsoft/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
|
|
Xabaril/AspNetCore.Diagnostics.HealthChecks
Enterprise HealthChecks for ASP.NET Core Diagnostics Package
|
|
|
oskardudycz/EventSourcing.NetCore
Examples and Tutorials of Event Sourcing in .NET
|
|
|
JasperFx/marten
.NET Transactional Document DB and Event Store on PostgreSQL
|
|
|
featbit/featbit
Enterprise-grade feature flag platform that you can self-host. Get started - free.
|
|
|
npgsql/efcore.pg
Entity Framework Core provider for PostgreSQL
|
|
|
Eventuous/eventuous
Event Sourcing library for .NET
|
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.3 | 83,357 | 5/27/2026 |
| 10.0.2 | 641,709 | 3/12/2026 |
| 10.0.1 | 1,049,964 | 12/19/2025 |
| 10.0.0 | 1,152,456 | 11/22/2025 |
| 10.0.0-rc.1 | 6,189 | 10/5/2025 |
| 9.0.5 | 20,147 | 3/12/2026 |
| 9.0.4 | 562,227 | 10/5/2025 |
| 9.0.3 | 3,300,591 | 2/24/2025 |
| 9.0.2 | 821,614 | 12/7/2024 |
| 9.0.1 | 109,149 | 11/19/2024 |
| 9.0.0 | 8,002 | 11/18/2024 |
| 8.0.9 | 15,679 | 3/12/2026 |
| 8.0.8 | 121,800 | 10/5/2025 |
| 8.0.7 | 313,378 | 2/24/2025 |
| 8.0.6 | 899,955 | 11/18/2024 |
| 8.0.5 | 1,065,405 | 10/13/2024 |
| 8.0.4 | 274,176 | 9/10/2024 |
| 7.0.10 | 15,281 | 3/17/2025 |
| 7.0.9 | 2,412 | 11/18/2024 |
| 7.0.8 | 43,890 | 9/10/2024 |