Skip to content
DocsTry Aspire
DocsTry

PostgresBuilderExtensions Methods

ClassMethods15 members
Provides extension methods for adding PostgreSQL resources to an Hosting.IDistributedApplicationBuilder.
AddDatabase(IResourceBuilder<PostgresServerResource>, string, string?)Section titled AddDatabase(IResourceBuilder<PostgresServerResource>, string, string?)extensionIResourceBuilder<PostgresDatabaseResource>
Adds a PostgreSQL database to the application model.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresDatabaseResource> AddDatabase(
this IResourceBuilder<PostgresServerResource> builder,
string name,
string? databaseName = null)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The PostgreSQL server resource builder.
namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.
databaseNamestring?optionalThe name of the database. If not provided, this defaults to the same value as name.
IResourceBuilder<PostgresDatabaseResource>A reference to the ApplicationModel.IResourceBuilder`1.

This resource includes built-in health checks. When this resource is referenced as a dependency using the ResourceBuilderExtensions.WaitFor extension method then the dependent resource will wait until the Postgres database is available.

Note that calling PostgresBuilderExtensions.AddDatabase will result in the database being created on the Postgres server when the server becomes ready. The database creation happens automatically as part of the resource lifecycle.

AddPostgres(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>, int?)Section titled AddPostgres(IDistributedApplicationBuilder, string, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>, int?)extensionIResourceBuilder<PostgresServerResource>
Adds a PostgreSQL resource to the application model. A container is used for local development.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> AddPostgres(
this IDistributedApplicationBuilder builder,
string name,
IResourceBuilder<ParameterResource>? userName = null,
IResourceBuilder<ParameterResource>? password = null,
int? port = null)
{
// ...
}
}
builderIDistributedApplicationBuilderThe Hosting.IDistributedApplicationBuilder.
namestringThe name of the resource. This name will be used as the connection string name when referenced in a dependency.
userNameIResourceBuilder<ParameterResource>optionalThe parameter used to provide the user name for the PostgreSQL resource. If null a default value will be used.
passwordIResourceBuilder<ParameterResource>optionalThe parameter used to provide the administrator password for the PostgreSQL resource. If null a random password will be generated.
portint?optionalThe host port used when launching the container. If null a random port will be assigned.
IResourceBuilder<PostgresServerResource>A reference to the ApplicationModel.IResourceBuilder`1.

This resource includes built-in health checks. When this resource is referenced as a dependency using the ResourceBuilderExtensions.WaitFor extension method then the dependent resource will wait until the Postgres resource is able to service requests.

This version of the package defaults to the tag of the container image.
WithCreationScript(IResourceBuilder<PostgresDatabaseResource>, string)Section titled WithCreationScript(IResourceBuilder<PostgresDatabaseResource>, string)extensionIResourceBuilder<PostgresDatabaseResource>
Defines the SQL script used to create the database.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresDatabaseResource> WithCreationScript(
this IResourceBuilder<PostgresDatabaseResource> builder,
string script)
{
// ...
}
}
builderIResourceBuilder<PostgresDatabaseResource>The builder for the PostgresDatabaseResource.
scriptstringThe SQL script used to create the database.
IResourceBuilder<PostgresDatabaseResource>A reference to the ApplicationModel.IResourceBuilder`1.
The script can only contain SQL statements applying to the default database like CREATE DATABASE. Custom statements like table creation and data insertion are not supported since they require a distinct connection to the newly created database. Default script is
CREATE DATABASE "<QUOTED_DATABASE_NAME>"
WithDataBindMount(IResourceBuilder<PostgresServerResource>, string, bool)Section titled WithDataBindMount(IResourceBuilder<PostgresServerResource>, string, bool)extensionIResourceBuilder<PostgresServerResource>
Adds a bind mount for the data folder to a PostgreSQL container resource.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithDataBindMount(
this IResourceBuilder<PostgresServerResource> builder,
string source,
bool isReadOnly = false)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
sourcestringThe source directory on the host to mount into the container.
isReadOnlybooloptionalA flag that indicates if this is a read-only mount.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.

The data directory location varies by PostgreSQL version:

  • PostgreSQL 17 and earlier: /var/lib/postgresql/data
  • PostgreSQL 18 and later: /var/lib/postgresql

This method automatically selects the correct path based on the configured container image tag.

WithDataVolume(IResourceBuilder<PostgresServerResource>, string?, bool)Section titled WithDataVolume(IResourceBuilder<PostgresServerResource>, string?, bool)extensionIResourceBuilder<PostgresServerResource>
Adds a named volume for the data folder to a PostgreSQL container resource.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithDataVolume(
this IResourceBuilder<PostgresServerResource> builder,
string? name = null,
bool isReadOnly = false)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
namestring?optionalThe name of the volume. Defaults to an auto-generated name based on the application and resource names.
isReadOnlybooloptionalA flag that indicates if this is a read-only volume.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.

The data directory location varies by PostgreSQL version:

  • PostgreSQL 17 and earlier: /var/lib/postgresql/data
  • PostgreSQL 18 and later: /var/lib/postgresql

This method automatically selects the correct path based on the configured container image tag.

WithHostPort(IResourceBuilder<PgAdminContainerResource>, int?)Section titled WithHostPort(IResourceBuilder<PgAdminContainerResource>, int?)extensionIResourceBuilder<PgAdminContainerResource>
Configures the host port that the PGAdmin resource is exposed on instead of using randomly assigned port.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PgAdminContainerResource> WithHostPort(
this IResourceBuilder<PgAdminContainerResource> builder,
int? port)
{
// ...
}
}
builderIResourceBuilder<PgAdminContainerResource>The resource builder for PGAdmin.
portint?The port to bind on the host. If null is used random port will be assigned.
IResourceBuilder<PgAdminContainerResource>The resource builder for PGAdmin.
WithHostPort(IResourceBuilder<PgWebContainerResource>, int?)Section titled WithHostPort(IResourceBuilder<PgWebContainerResource>, int?)extensionIResourceBuilder<PgWebContainerResource>
Configures the host port that the pgweb resource is exposed on instead of using randomly assigned port.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PgWebContainerResource> WithHostPort(
this IResourceBuilder<PgWebContainerResource> builder,
int? port)
{
// ...
}
}
builderIResourceBuilder<PgWebContainerResource>The resource builder for pgweb.
portint?The port to bind on the host. If null is used random port will be assigned.
IResourceBuilder<PgWebContainerResource>The resource builder for pgweb.
WithHostPort(IResourceBuilder<PostgresServerResource>, int?)Section titled WithHostPort(IResourceBuilder<PostgresServerResource>, int?)extensionIResourceBuilder<PostgresServerResource>
Configures the host port that the PostgreSQL resource is exposed on instead of using randomly assigned port.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithHostPort(
this IResourceBuilder<PostgresServerResource> builder,
int? port)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
portint?The port to bind on the host. If null is used random port will be assigned.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.
WithInitBindMount(IResourceBuilder<PostgresServerResource>, string, bool)Section titled WithInitBindMount(IResourceBuilder<PostgresServerResource>, string, bool)extensionIResourceBuilder<PostgresServerResource>
Adds a bind mount for the init folder to a PostgreSQL container resource.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithInitBindMount(
this IResourceBuilder<PostgresServerResource> builder,
string source,
bool isReadOnly = true)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
sourcestringThe source directory on the host to mount into the container.
isReadOnlybooloptionalA flag that indicates if this is a read-only mount.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.
This method is not available in polyglot app hosts. Use PostgresBuilderExtensions.WithInitFiles instead.
WithInitFiles(IResourceBuilder<PostgresServerResource>, string)Section titled WithInitFiles(IResourceBuilder<PostgresServerResource>, string)extensionIResourceBuilder<PostgresServerResource>
Copies init files to a PostgreSQL container resource.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithInitFiles(
this IResourceBuilder<PostgresServerResource> builder,
string source)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
sourcestringThe source directory or files on the host to copy into the container.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.
WithPassword(IResourceBuilder<PostgresServerResource>, IResourceBuilder<ParameterResource>)Section titled WithPassword(IResourceBuilder<PostgresServerResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<PostgresServerResource>
Configures the password that the PostgreSQL resource is used.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithPassword(
this IResourceBuilder<PostgresServerResource> builder,
IResourceBuilder<ParameterResource> password)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
passwordIResourceBuilder<ParameterResource>The parameter used to provide the password for the PostgreSQL resource.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.
WithPgAdmin(IResourceBuilder<T>, Action<IResourceBuilder<PgAdminContainerResource>>, string?)Section titled WithPgAdmin(IResourceBuilder<T>, Action<IResourceBuilder<PgAdminContainerResource>>, string?)extensionIResourceBuilder<T>
Adds a pgAdmin 4 administration and development platform for PostgreSQL to the application model.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<T> WithPgAdmin<T>(
this IResourceBuilder<T> builder,
Action<IResourceBuilder<PgAdminContainerResource>>? configureContainer = null,
string? containerName = null)
{
// ...
}
}
builderIResourceBuilder<T>The PostgreSQL server resource builder.
configureContainerAction<IResourceBuilder<PgAdminContainerResource>>optionalCallback to configure PgAdmin container resource.
containerNamestring?optionalThe name of the container (Optional).
IResourceBuilder<T>A reference to the ApplicationModel.IResourceBuilder`1.
This version of the package defaults to the tag of the container image.
WithPgWeb(IResourceBuilder<PostgresServerResource>, Action<IResourceBuilder<PgWebContainerResource>>, string?)Section titled WithPgWeb(IResourceBuilder<PostgresServerResource>, Action<IResourceBuilder<PgWebContainerResource>>, string?)extensionIResourceBuilder<PostgresServerResource>
Adds an administration and development platform for PostgreSQL to the application model using pgweb.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithPgWeb(
this IResourceBuilder<PostgresServerResource> builder,
Action<IResourceBuilder<PgWebContainerResource>>? configureContainer = null,
string? containerName = null)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The Postgres server resource builder.
configureContainerAction<IResourceBuilder<PgWebContainerResource>>optionalConfiguration callback for pgweb container resource.
containerNamestring?optionalThe name of the container (Optional).
IResourceBuilder<PostgresServerResource>A reference to the ApplicationModel.IResourceBuilder`1.
This version of the package defaults to the tag of the container image.
WithPostgresMcp(IResourceBuilder<PostgresDatabaseResource>, Action<IResourceBuilder<PostgresMcpContainerResource>>, string?)Section titled WithPostgresMcp(IResourceBuilder<PostgresDatabaseResource>, Action<IResourceBuilder<PostgresMcpContainerResource>>, string?)extensionIResourceBuilder<PostgresDatabaseResource>
Adds a Postgres MCP server container and configures it to connect to the database represented by builder.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresDatabaseResource> WithPostgresMcp(
this IResourceBuilder<PostgresDatabaseResource> builder,
Action<IResourceBuilder<PostgresMcpContainerResource>>? configureContainer = null,
string? containerName = null)
{
// ...
}
}
builderIResourceBuilder<PostgresDatabaseResource>The Postgres database resource builder.
configureContainerAction<IResourceBuilder<PostgresMcpContainerResource>>optionalConfiguration callback for the Postgres MCP container resource.
containerNamestring?optionalThe name of the container (optional).
IResourceBuilder<PostgresDatabaseResource>A reference to the ApplicationModel.IResourceBuilder`1.
The Postgres MCP server is configured to use SSE transport and will expose an HTTP endpoint. This version of the package defaults to the tag of the container image.
WithUserName(IResourceBuilder<PostgresServerResource>, IResourceBuilder<ParameterResource>)Section titled WithUserName(IResourceBuilder<PostgresServerResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<PostgresServerResource>
Configures the user name that the PostgreSQL resource is used.
public static class PostgresBuilderExtensions
{
public static IResourceBuilder<PostgresServerResource> WithUserName(
this IResourceBuilder<PostgresServerResource> builder,
IResourceBuilder<ParameterResource> userName)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The resource builder.
userNameIResourceBuilder<ParameterResource>The parameter used to provide the user name for the PostgreSQL resource.
IResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1.