Overview
TheAspire.Npgsql component registers a NpgsqlDataSource in your dependency injection container for connecting to PostgreSQL databases. It automatically enables health checks, metrics, logging, and distributed tracing.
Installation
Install the component using the .NET CLI:Usage
Register the component
In your service’sProgram.cs file, call the AddNpgsqlDataSource extension method:
Inject and use the data source
Retrieve theNpgsqlDataSource instance using dependency injection:
Configuration
The component provides multiple configuration options based on your project requirements.Connection string
Provide a connection string in yourappsettings.json:
Configuration providers
Configure component settings using theAspire:Npgsql configuration section:
Inline configuration
Configure settings directly in code using a delegate:AppHost integration
In your AppHost project, install the PostgreSQL hosting package:Program.cs:
WithReference method automatically configures the connection string in your service. Consume it in your service’s Program.cs:
The connection name passed to
WithReference must match the name used in AddNpgsqlDataSource.Configuration options
The following settings are available:| Setting | Description | Default |
|---|---|---|
DisableHealthChecks | Disable automatic health check registration | false |
DisableTracing | Disable OpenTelemetry tracing | false |
DisableMetrics | Disable OpenTelemetry metrics | false |
DisableLogging | Disable logging | false |
Health checks
The component automatically registers a health check that verifies the database connection. The health check:- Opens a connection to the database
- Executes a simple query to verify connectivity
- Reports the status in the
/healthendpoint
DisableHealthChecks to true.
Observability
Logging
The component integrates with .NET logging to provide:- Connection pool events
- Command execution information
- Error details
Metrics
The following metrics are collected:- Active connections
- Connection pool statistics
- Command execution duration
- Query count
Tracing
Distributed tracing captures:- Database operations
- Query execution time
- Connection pool activity
Best practices
Use connection pooling
Use connection pooling
NpgsqlDataSource includes built-in connection pooling. Reuse the same data source instance throughout your application instead of creating new connections for each operation.Dispose connections properly
Dispose connections properly
Always use
await using when working with connections and commands to ensure proper disposal:Use parameterized queries
Use parameterized queries
Protect against SQL injection by using parameterized queries:
Configure timeouts appropriately
Configure timeouts appropriately
Set command timeouts based on your expected query execution times:
Additional resources
Related components
SQL Server
Connect to SQL Server databases
MongoDB
Work with MongoDB databases
Redis
Add caching with Redis
PostgreSQL Hosting
Learn about the PostgreSQL hosting integration