Skip to content
Docs Try Aspire
Docs Try

Get started with the PostgreSQL integrations

PostgreSQL logo

PostgreSQL is a powerful, open source object-relational database system with many years of active development that has earned it a strong reputation for reliability, feature robustness, and performance. The Aspire PostgreSQL integration provides a way to connect to existing PostgreSQL databases, or create new instances from the docker.io/library/postgres container image.

Use this guide to add PostgreSQL to an Aspire solution. Choose the AppHost language for your solution first, then follow the matching quickstart. For the full PostgreSQL hosting API surface in either C# or TypeScript, see PostgreSQL Hosting integration.

Choose your AppHost language, then add PostgreSQL to the AppHost. For the full C# and TypeScript hosting API surface, see PostgreSQL Hosting integration.

Select your AppHost language

Use this path when your Aspire solution is modeled in apphost.ts.

Add the PostgreSQL Hosting integration with the Aspire CLI

Section titled “Add the PostgreSQL Hosting integration with the Aspire CLI”
Terminal
aspire add postgres

This updates your aspire.config.json with the PostgreSQL hosting integration package:

aspire.config.json
{
"packages": {
"Aspire.Hosting.PostgreSQL": "*"
}
}
apphost.ts
import { createBuilder } from './.modules/aspire.js';
const builder = await createBuilder();
const postgres = await builder.addPostgres("postgres");
const postgresdb = await postgres.addDatabase("postgresdb");
await builder.addNodeApp("api", "./api", "index.js")
.withExternalHttpEndpoints()
.waitFor(postgresdb)
.withReference(postgresdb);

The examples below assume your apphost.ts file references postgresdb from the consuming app with withReference(postgresdb).

Install the PostgreSQL client library in your JavaScript or TypeScript consuming project:

Terminal
npm install pg

Import pg in code files that interact with the database:

JavaScript - Import pg
import pg from 'pg';

Use the process.env object to read the injected PostgreSQL properties:

JavaScript - Obtain configuration properties
const pguser = process.env.POSTGRESDB_USERNAME;
const pgpassword = process.env.POSTGRESDB_PASSWORD;
const pghost = process.env.POSTGRESDB_HOST;
const pgport = process.env.POSTGRESDB_PORT;
const pgdatabase = process.env.POSTGRESDB_DATABASENAME;

Connect to PostgreSQL with pg:

JavaScript - Connect to PostgreSQL
const client = new pg.Client({
user: pguser,
host: pghost,
database: pgdatabase,
password: pgpassword,
port: pgport,
});
await client.connect();

Use this path when your Aspire solution is modeled in AppHost.cs.

Add the PostgreSQL Hosting integration to the AppHost

Section titled “Add the PostgreSQL Hosting integration to the AppHost”
Terminal
aspire add postgres

Or, add the package manually in either AppHost.cs or AppHost.csproj:

C# - AppHost.cs
#:package Aspire.Hosting.PostgreSQL@*
XML - AppHost.csproj
<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="*" />
AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres");
var postgresdb = postgres.AddDatabase("postgresdb");
var api = builder.AddProject<Projects.ExampleProject>("apiservice")
.WaitFor(postgresdb)
.WithReference(postgresdb);

The examples below assume your AppHost.cs file references postgresdb from the consuming app with WithReference(postgresdb).

Install the Aspire PostgreSQL client integration in each C# consuming project:

.NET CLI — Add Aspire.Npgsql package
dotnet add package Aspire.Npgsql

In Program.cs, call the AddNpgsqlDataSource extension method on an IHostApplicationBuilder to register an NpgsqlDataSource for use via dependency injection:

C# - Program.cs
builder.AddNpgsqlDataSource(connectionName: "postgresdb");

Get the NpgsqlDataSource instance by using dependency injection:

C# - ExampleService.cs
public class ExampleService(NpgsqlDataSource dataSource)
{
// Use dataSource to query the database...
}

Now that you have an Aspire app with PostgreSQL integrations up and running, you can use the following reference documents to learn how to configure and interact with the PostgreSQL resources: