Installation
Install the PostgreSQL hosting integration package in your AppHost project:Usage
Add PostgreSQL Server and Database
In your AppHost project (AppHost.cs), add a PostgreSQL server and database:
Consume in Service
In your service project, reference the database using the client integration:Program.cs:
API Reference
AddPostgres
Adds a PostgreSQL server resource to the application model. A container is used for local development.Parameters
name- The name of the resource. This name will be used as the connection string name when referenced in a dependency.userName- The parameter used to provide the user name for the PostgreSQL resource. Ifnulla default value will be used.password- The parameter used to provide the administrator password for the PostgreSQL resource. Ifnulla random password will be generated.port- The host port used when launching the container. If null a random port will be assigned.
Returns
A reference to theIResourceBuilder<PostgresServerResource>.
AddDatabase
Adds a PostgreSQL database to the application model.Parameters
name- The name of the resource. This name will be used as the connection string name when referenced in a dependency.databaseName- The name of the database. If not provided, defaults to the same value asname.
Configuration Examples
Custom Port
Custom Password
Data Persistence with Volumes
Multiple Databases
PgAdmin Management Tool
Named Volumes
Bind Mounts
Connection Properties
When you reference a PostgreSQL resource usingWithReference, the following connection properties are made available to the consuming project.
PostgreSQL Server
| Property Name | Description |
|---|---|
Host | The hostname or IP address of the PostgreSQL server |
Port | The port number the PostgreSQL server is listening on |
Username | The username for authentication |
Password | The password for authentication |
Uri | The connection URI in postgresql:// format, with the format postgresql://{Username}:{Password}@{Host}:{Port} |
JdbcConnectionString | JDBC-format connection string, with the format jdbc:postgresql://{Host}:{Port}. User and password credentials are provided as separate Username and Password properties. |
PostgreSQL Database
The PostgreSQL database resource inherits all properties from its parentPostgresServerResource and adds:
| Property Name | Description |
|---|---|
Uri | The connection URI with the database name, with the format postgresql://{Username}:{Password}@{Host}:{Port}/{DatabaseName} |
JdbcConnectionString | JDBC connection string with database name, with the format jdbc:postgresql://{Host}:{Port}/{DatabaseName}. User and password credentials are provided as separate Username and Password properties. |
DatabaseName | The name of the database |
Environment Variables
Aspire exposes each property as an environment variable named[RESOURCE]_[PROPERTY]. For instance:
- The
Uriproperty of a resource calledmydbbecomesMYDB_URI - The
DatabaseNameproperty becomesMYDB_DATABASENAME
MCP (Model Context Protocol) Support
The PostgreSQL hosting integration provides support for adding an MCP sidecar container that enables AI agents to interact with PostgreSQL databases.Health Checks
The PostgreSQL hosting integration includes built-in health checks. When a resource is referenced as a dependency usingWaitFor, the dependent resource will wait until the PostgreSQL resource is able to service requests.