PostgreSQL Hosting integration
To get started with the Aspire PostgreSQL integrations, follow the Get started with PostgreSQL integrations guide. If you want to learn how to use the PostgreSQL Entity Framework Core (EF Core) client integration, see Get started with the PostgreSQL Entity Framework Core integrations.
This article includes full details on the Aspire PostgreSQL Hosting integration, with examples for both AppHost.cs and apphost.ts, so you can model PostgreSQL server and database resources in your AppHost project.
Use this selector to switch the C# and TypeScript examples throughout the page.
Installation
Section titled “Installation”The PostgreSQL hosting integration models various PostgreSQL resources as the following types.
PostgresServerResourcePostgresDatabaseResourcePostgresPgAdminContainerResourcePostgresPgWebContainerResource
To access these types and APIs for expressing them as resources in your AppHost project, install the 📦 Aspire.Hosting.PostgreSQL NuGet package:
aspire add postgresOr, choose a manual installation approach:
#:package Aspire.Hosting.PostgreSQL@*<PackageReference Include="Aspire.Hosting.PostgreSQL" Version="*" />aspire add postgresThis updates your aspire.config.json with the PostgreSQL hosting integration package:
{ "packages": { "Aspire.Hosting.PostgreSQL": "13.2.0" }}Add PostgreSQL server resource
Section titled “Add PostgreSQL server resource”In your AppHost project, add a PostgreSQL server resource and then add a database resource as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres");var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>("apiservice") .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...-
When Aspire adds a container image to the app host, as shown in the preceding example with the
docker.io/library/postgresimage, it creates a new PostgreSQL server instance on your local machine. A reference to thepostgresdbdatabase resource is then used to add a dependency to the consuming project. -
When adding a database resource to the app model, the database is created if it doesn’t already exist. The creation of the database relies on the AppHost eventing APIs, specifically
ResourceReadyEvent. In other words, when thepostgresresource is ready, the event is raised and the database resource is created. -
The PostgreSQL server resource includes default credentials with a
usernameof"postgres"and a randomly generatedpasswordusing theCreateDefaultPasswordParametermethod. -
The AppHost reference call configures a connection in the consuming project named after the referenced database resource, such as
postgresdbin the preceding example.
Add PostgreSQL resource with database scripts
Section titled “Add PostgreSQL resource with database scripts”By default, when you add a PostgresDatabaseResource, it relies on the following script to create the database:
CREATE DATABASE "<QUOTED_DATABASE_NAME>"To alter the default script, configure the database resource with a custom creation script:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres");
var databaseName = "app_db";var creationScript = $$""" -- Create the database CREATE DATABASE {{databaseName}};
""";
var db = postgres.AddDatabase(databaseName) .WithCreationScript(creationScript);
builder.AddProject<Projects.ExampleProject>() .WithReference(db) .WaitFor(db);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");
const const databaseName: "app_db"
databaseName = "app_db";const const creationScript: "\n-- Create the database\nCREATE DATABASE app_db;\n"
creationScript = `-- Create the databaseCREATE DATABASE ${const databaseName: "app_db"
databaseName};`;
const const db: PostgresDatabaseResource
db = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase(const databaseName: "app_db"
databaseName);await const db: PostgresDatabaseResource
db.PostgresDatabaseResource.withCreationScript(script: string): PostgresDatabaseResource
Defines the SQL script for database creation
withCreationScript(const creationScript: "\n-- Create the database\nCREATE DATABASE app_db;\n"
creationScript);
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const db: PostgresDatabaseResource
db) .ExecutableResource.waitFor(dependency: IResource): NodeAppResource
Waits for another resource to be ready
waitFor(const db: PostgresDatabaseResource
db);
// After adding all resources, run the app...The preceding example creates a database named app_db. The script is run when the database resource is created. The script is passed as a string to the database resource’s creation-script API and then run in the context of the PostgreSQL resource.
Add PostgreSQL pgAdmin resource
Section titled “Add PostgreSQL pgAdmin resource”Add the dpage/pgadmin4 container to the PostgreSQL resource to get a web-based admin dashboard, as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithPgAdmin();
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withPgAdmin(options?: { configureContainer?: ((obj: PgAdminContainerResource) => Promise<void>) | undefined; containerName?: string;} | undefined): PostgresServerResource (+1 overload)
Adds pgAdmin 4 management UI
withPgAdmin();
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...The preceding code adds a container based on the docker.io/dpage/pgadmin4 image. The container is used to manage the PostgreSQL server and database resources and serves a web-based admin dashboard for PostgreSQL databases.
Configure the pgAdmin host port
Section titled “Configure the pgAdmin host port”To configure the host port for the pgAdmin container, configure the pgAdmin resource inside the callback as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithPgAdmin(pgAdmin => pgAdmin.WithHostPort(5050));
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withPgAdmin(options?: { configureContainer?: ((obj: PgAdminContainerResource) => Promise<void>) | undefined; containerName?: string;} | undefined): PostgresServerResource (+1 overload)
Adds pgAdmin 4 management UI
withPgAdmin({ configureContainer?: ((obj: PgAdminContainerResource) => Promise<void>) | undefined
configureContainer: async pgAdmin: PgAdminContainerResource
pgAdmin => { await pgAdmin: PgAdminContainerResource
pgAdmin.PgAdminContainerResource.withHostPort(port: number | null): PgAdminContainerResource
Sets the host port for pgAdmin
withHostPort(5050); }});
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...The preceding code adds and configures the host port for the pgAdmin container. The host port is otherwise randomly assigned.
Add PostgreSQL pgWeb resource
Section titled “Add PostgreSQL pgWeb resource”Add the sosedoff/pgweb container to the PostgreSQL resource to get a web-based admin dashboard, as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithPgWeb();
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withPgWeb(options?: { configureContainer?: ((obj: PgWebContainerResource) => Promise<void>) | undefined; containerName?: string;} | undefined): PostgresServerResource (+1 overload)
Adds pgweb management UI
withPgWeb();
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...The preceding code adds a container based on the docker.io/sosedoff/pgweb image. All registered PostgresDatabaseResource instances are used to create a configuration file per instance, and each config is bound to the pgweb container bookmark directory. For more information, see PgWeb docs: Server connection bookmarks.
Configure the pgWeb host port
Section titled “Configure the pgWeb host port”To configure the host port for the pgWeb container, configure the pgWeb resource inside the callback as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithPgWeb(pgWeb => pgWeb.WithHostPort(5050));
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withPgWeb(options?: { configureContainer?: ((obj: PgWebContainerResource) => Promise<void>) | undefined; containerName?: string;} | undefined): PostgresServerResource (+1 overload)
Adds pgweb management UI
withPgWeb({ configureContainer?: ((obj: PgWebContainerResource) => Promise<void>) | undefined
configureContainer: async pgWeb: PgWebContainerResource
pgWeb => { await pgWeb: PgWebContainerResource
pgWeb.PgWebContainerResource.withHostPort(port: number | null): PgWebContainerResource
Sets the host port for pgweb
withHostPort(5050); }});
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...The preceding code adds and configures the host port for the pgWeb container. The host port is otherwise randomly assigned.
Add PostgreSQL server resource with data volume
Section titled “Add PostgreSQL server resource with data volume”Add a data volume to the PostgreSQL server resource as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithDataVolume(isReadOnly: false);
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withDataVolume(options?: { name?: string; isReadOnly?: boolean;} | undefined): PostgresServerResource (+1 overload)
Adds a data volume for PostgreSQL
withDataVolume({ isReadOnly?: boolean | undefined
isReadOnly: false });
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...The data volume is used to persist the PostgreSQL server data outside the lifecycle of its container. The data volume is mounted at the /var/lib/postgresql/data path in the PostgreSQL server container and when a name parameter isn’t provided, the name is generated at random. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.
Add PostgreSQL server resource with data bind mount
Section titled “Add PostgreSQL server resource with data bind mount”Add a data bind mount to the PostgreSQL server resource as shown in the following examples:
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithDataBindMount( source: "/PostgreSQL/Data", isReadOnly: false);
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withDataBindMount(source: string, options?: { isReadOnly?: boolean;} | undefined): PostgresServerResource (+1 overload)
Adds a data bind mount for PostgreSQL
withDataBindMount("/PostgreSQL/Data", { isReadOnly?: boolean | undefined
isReadOnly: false });
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...Data bind mounts rely on the host machine’s filesystem to persist the PostgreSQL server data across container restarts. The data bind mount is mounted at the C:\PostgreSQL\Data on Windows (or /PostgreSQL/Data on Unix) path on the host machine in the PostgreSQL server container. For more information on data bind mounts, see Docker docs: Bind mounts.
Add PostgreSQL server resource with init bind mount
Section titled “Add PostgreSQL server resource with init bind mount”Use initialization files to seed the PostgreSQL server. The C# AppHost exposes WithInitBindMount(...), while the TypeScript AppHost exposes withInitFiles(...).
var builder = DistributedApplication.CreateBuilder(args);
var postgres = builder.AddPostgres("postgres") .WithInitBindMount(@"C:\PostgreSQL\Init");
var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres");await const postgres: PostgresServerResource
postgres.PostgresServerResource.withInitFiles(source: string): PostgresServerResource
Copies init files to PostgreSQL
withInitFiles("./postgres-init");
const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...The TypeScript AppHost doesn’t currently expose a withInitBindMount(...) API for PostgreSQL. Use withInitFiles(...) when you want to copy initialization files into the container instead.
The init bind mount relies on the host machine’s filesystem to initialize the PostgreSQL server database with the container’s init folder. This folder is used for initialization, running any executable shell scripts or .sql command files after the postgres-data folder is created. The init bind mount is mounted at the C:\PostgreSQL\Init on Windows (or /PostgreSQL/Init on Unix) path on the host machine in the PostgreSQL server container.
Add PostgreSQL server resource with parameters
Section titled “Add PostgreSQL server resource with parameters”When you want to explicitly provide the username and password used by the container image, you can provide these credentials as parameters. Consider the following alternative examples:
var builder = DistributedApplication.CreateBuilder(args);
var username = builder.AddParameter("username", secret: true);var password = builder.AddParameter("password", secret: true);
var postgres = builder.AddPostgres("postgres", username, password);var postgresdb = postgres.AddDatabase("postgresdb");
var exampleProject = builder.AddProject<Projects.ExampleProject>() .WithReference(postgresdb);
// After adding all resources, run the app...import { function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder } from './.modules/aspire.js';
const const builder: IDistributedApplicationBuilder
builder = await function createBuilder(): IDistributedApplicationBuilder
Creates a new distributed application builder
createBuilder();
const const userName: ParameterResource
userName = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addParameter(name: string, options?: { secret?: boolean;}): ParameterResource (+1 overload)
Adds a parameter resource
addParameter("username", { secret?: boolean | undefined
secret: true });const const password: ParameterResource
password = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addParameter(name: string, options?: { secret?: boolean;}): ParameterResource (+1 overload)
Adds a parameter resource
addParameter("password", { secret?: boolean | undefined
secret: true });
const const postgres: PostgresServerResource
postgres = await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addPostgres(name: string, options?: { userName?: string | ParameterResource; password?: string | ParameterResource; port?: number;}): PostgresServerResource (+1 overload)
Adds a PostgreSQL server resource
addPostgres("postgres", { userName?: string | ParameterResource | undefined
userName, password?: string | ParameterResource | undefined
password });const const postgresdb: PostgresDatabaseResource
postgresdb = await const postgres: PostgresServerResource
postgres.PostgresServerResource.addDatabase(name: string, options?: { databaseName?: string;} | undefined): PostgresDatabaseResource (+1 overload)
Adds a PostgreSQL database
addDatabase("postgresdb");
await const builder: IDistributedApplicationBuilder
builder.IDistributedApplicationBuilder.addNodeApp(name: string, appDirectory: string, scriptPath: string): NodeAppResource
Adds a Node.js application resource
addNodeApp("api", "./api", "index.js") .ExecutableResource.withReference(source: IResource, options?: { connectionName?: string; optional?: boolean; name?: string;} | undefined): NodeAppResource (+1 overload)
Adds a reference to another resource
withReference(const postgresdb: PostgresDatabaseResource
postgresdb);
// After adding all resources, run the app...Connection properties
Section titled “Connection properties”For a full reference of PostgreSQL connection properties and environment variables — including how to connect from JavaScript, Python, Go, and .NET — see Connect to PostgreSQL.
Hosting integration health checks
Section titled “Hosting integration health checks”The PostgreSQL hosting integration automatically adds a health check for the PostgreSQL server resource. The health check verifies that the PostgreSQL server is running and that a connection can be established to it.
The hosting integration relies on the 📦 AspNetCore.HealthChecks.Npgsql NuGet package.