Get started with the PostgreSQL integrations
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.
Set up the PostgreSQL Hosting integration
Section titled “Set up the 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.
TypeScript AppHost quickstart
Section titled “TypeScript AppHost quickstart”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”aspire add postgresThis updates your aspire.config.json with the PostgreSQL hosting integration package:
{ "packages": { "Aspire.Hosting.PostgreSQL": "*" }}Model PostgreSQL resources in apphost.ts
Section titled “Model PostgreSQL resources in 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);Connect from your consuming app
Section titled “Connect from your consuming app”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:
npm install pgImport pg in code files that interact with the database:
import pg from 'pg';Use the process.env object to read the injected PostgreSQL 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:
const client = new pg.Client({ user: pguser, host: pghost, database: pgdatabase, password: pgpassword, port: pgport,});
await client.connect();Install a PostgreSQL driver in your Python consuming project. This example uses psycopg:
pip install psycopg[binary]Import os and psycopg in code files that interact with the database:
import osimport psycopgUse os.getenv() to read the injected PostgreSQL properties:
postgres_uri = os.getenv("POSTGRESDB_URI")postgres_name = os.getenv("POSTGRESDB_DATABASENAME")Connect to PostgreSQL with psycopg:
async with await psycopg.AsyncConnection.connect(postgres_uri, autocommit=True) as conn:Install the Aspire PostgreSQL client integration in each C# consuming project:
dotnet add package Aspire.Npgsql#:package Aspire.Npgsql@*<PackageReference Include="Aspire.Npgsql" Version="*" />In Program.cs, call the AddNpgsqlDataSource extension method on an IHostApplicationBuilder to register an NpgsqlDataSource for use via dependency injection:
builder.AddNpgsqlDataSource(connectionName: "postgresdb");Get the NpgsqlDataSource instance by using dependency injection:
public class ExampleService(NpgsqlDataSource dataSource){ // Use dataSource to query the database...}C# AppHost quickstart
Section titled “C# AppHost quickstart”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”aspire add postgresOr, add the package manually in either AppHost.cs or AppHost.csproj:
#:package Aspire.Hosting.PostgreSQL@*<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="*" />Model PostgreSQL resources in AppHost.cs
Section titled “Model PostgreSQL resources in 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);Connect from your consuming app
Section titled “Connect from your consuming app”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:
dotnet add package Aspire.Npgsql#:package Aspire.Npgsql@*<PackageReference Include="Aspire.Npgsql" Version="*" />In Program.cs, call the AddNpgsqlDataSource extension method on an IHostApplicationBuilder to register an NpgsqlDataSource for use via dependency injection:
builder.AddNpgsqlDataSource(connectionName: "postgresdb");Get the NpgsqlDataSource instance by using dependency injection:
public class ExampleService(NpgsqlDataSource dataSource){ // Use dataSource to query the database...}Install a PostgreSQL driver in your Python consuming project. This example uses psycopg:
pip install psycopg[binary]Import os and psycopg in code files that interact with the database:
import osimport psycopgUse os.getenv() to read the injected PostgreSQL properties:
postgres_uri = os.getenv("POSTGRESDB_URI")postgres_name = os.getenv("POSTGRESDB_DATABASENAME")Connect to PostgreSQL with psycopg:
async with await psycopg.AsyncConnection.connect(postgres_uri, autocommit=True) as conn:Install the PostgreSQL client library in your JavaScript or TypeScript consuming project:
npm install pgImport pg in code files that interact with the database:
import pg from 'pg';Use the process.env object to read the injected PostgreSQL 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:
const client = new pg.Client({ user: pguser, host: pghost, database: pgdatabase, password: pgpassword, port: pgport,});
await client.connect();Next steps
Section titled “Next steps”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: