Skip to content
DocsTry Aspire
DocsTry

AzurePostgresExtensions Methods

ClassMethods8 members
Provides extension methods for adding the Azure PostgreSQL resources to the application model.
AddAzurePostgresFlexibleServer(IDistributedApplicationBuilder, string)Section titled AddAzurePostgresFlexibleServer(IDistributedApplicationBuilder, string)extensionIResourceBuilder<AzurePostgresFlexibleServerResource>
Adds an Azure PostgreSQL Flexible Server resource to the application model.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<AzurePostgresFlexibleServerResource> AddAzurePostgresFlexibleServer(
this IDistributedApplicationBuilder builder,
string name)
{
// ...
}
}
builderIDistributedApplicationBuilderThe builder for the distributed application.
namestringThe name of the resource.
IResourceBuilder<AzurePostgresFlexibleServerResource>A reference to the ApplicationModel.IResourceBuilder`1 builder.
By default, the Azure PostgreSQL Flexible Server resource is configured to use Microsoft Entra ID (Azure Active Directory) for authentication. This requires changes to the application code to use an azure credential to authenticate with the resource. See https://learn.microsoft.com/azure/postgresql/flexible-server/how-to-connect-with-managed-identity#connect-using-managed-identity-in-c for more information. You can use the AzurePostgresExtensions.WithPasswordAuthentication method to configure the resource to use password authentication. The following example creates an Azure PostgreSQL Flexible Server resource and referencing that resource in a .NET project.
var builder = DistributedApplication.CreateBuilder(args);
var data = builder.AddAzurePostgresFlexibleServer("data");
builder.AddProject<Projects.ProductService>()
.WithReference(data);
builder.Build().Run();
AddDatabase(IResourceBuilder<AzurePostgresFlexibleServerResource>, string, string?)Section titled AddDatabase(IResourceBuilder<AzurePostgresFlexibleServerResource>, string, string?)extensionIResourceBuilder<AzurePostgresFlexibleServerDatabaseResource>
Adds an Azure PostgreSQL database to the application model.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource> AddDatabase(
this IResourceBuilder<AzurePostgresFlexibleServerResource> builder,
string name,
string? databaseName = null)
{
// ...
}
}
builderIResourceBuilder<AzurePostgresFlexibleServerResource>The Azure 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<AzurePostgresFlexibleServerDatabaseResource>A reference to the ApplicationModel.IResourceBuilder`1.
AsAzurePostgresFlexibleServer(IResourceBuilder<PostgresServerResource>)Section titled AsAzurePostgresFlexibleServer(IResourceBuilder<PostgresServerResource>)extensionIResourceBuilder<PostgresServerResource>
Configures resource to use Azure for local development and when doing a deployment via the Azure Developer CLI.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<PostgresServerResource> AsAzurePostgresFlexibleServer(
this IResourceBuilder<PostgresServerResource> builder)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1 builder.
IResourceBuilder<PostgresServerResource>A reference to the ApplicationModel.IResourceBuilder`1 builder.
PublishAsAzurePostgresFlexibleServer(IResourceBuilder<PostgresServerResource>)Section titled PublishAsAzurePostgresFlexibleServer(IResourceBuilder<PostgresServerResource>)extensionIResourceBuilder<PostgresServerResource>
Configures Postgres Server resource to be deployed as Azure PostgreSQL Flexible Server.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<PostgresServerResource> PublishAsAzurePostgresFlexibleServer(
this IResourceBuilder<PostgresServerResource> builder)
{
// ...
}
}
builderIResourceBuilder<PostgresServerResource>The ApplicationModel.IResourceBuilder`1 builder.
IResourceBuilder<PostgresServerResource>A reference to the ApplicationModel.IResourceBuilder`1 builder.
RunAsContainer(IResourceBuilder<AzurePostgresFlexibleServerResource>, Action<IResourceBuilder<PostgresServerResource>>)Section titled RunAsContainer(IResourceBuilder<AzurePostgresFlexibleServerResource>, Action<IResourceBuilder<PostgresServerResource>>)extensionIResourceBuilder<AzurePostgresFlexibleServerResource>
Configures an Azure PostgreSQL Flexible Server resource to run locally in a container.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<AzurePostgresFlexibleServerResource> RunAsContainer(
this IResourceBuilder<AzurePostgresFlexibleServerResource> builder,
Action<IResourceBuilder<PostgresServerResource>>? configureContainer = null)
{
// ...
}
}
builderIResourceBuilder<AzurePostgresFlexibleServerResource>The Azure PostgreSQL server resource builder.
configureContainerAction<IResourceBuilder<PostgresServerResource>>optionalCallback that exposes underlying container to allow for customization.
IResourceBuilder<AzurePostgresFlexibleServerResource>A reference to the ApplicationModel.IResourceBuilder`1 builder.
The following example creates an Azure PostgreSQL Flexible Server resource that runs locally in a PostgreSQL container and referencing that resource in a .NET project.
var builder = DistributedApplication.CreateBuilder(args);
var data = builder.AddAzurePostgresFlexibleServer("data")
.RunAsContainer();
builder.AddProject<Projects.ProductService>()
.WithReference(data);
builder.Build().Run();
WithPasswordAuthentication(IResourceBuilder<AzurePostgresFlexibleServerResource>, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)Section titled WithPasswordAuthentication(IResourceBuilder<AzurePostgresFlexibleServerResource>, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<AzurePostgresFlexibleServerResource>
Configures the resource to use password authentication for Azure PostgreSQL Flexible Server.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<AzurePostgresFlexibleServerResource> WithPasswordAuthentication(
this IResourceBuilder<AzurePostgresFlexibleServerResource> builder,
IResourceBuilder<ParameterResource>? userName = null,
IResourceBuilder<ParameterResource>? password = null)
{
// ...
}
}
builderIResourceBuilder<AzurePostgresFlexibleServerResource>The Azure PostgreSQL server resource builder.
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.
IResourceBuilder<AzurePostgresFlexibleServerResource>A reference to the ApplicationModel.IResourceBuilder`1 builder.
The following example creates an Azure PostgreSQL Flexible Server resource that uses password authentication.
var builder = DistributedApplication.CreateBuilder(args);
var data = builder.AddAzurePostgresFlexibleServer("data")
.WithPasswordAuthentication();
builder.AddProject<Projects.ProductService>()
.WithReference(data);
builder.Build().Run();
WithPasswordAuthentication(IResourceBuilder<AzurePostgresFlexibleServerResource>, IResourceBuilder<IAzureKeyVaultResource>, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)Section titled WithPasswordAuthentication(IResourceBuilder<AzurePostgresFlexibleServerResource>, IResourceBuilder<IAzureKeyVaultResource>, IResourceBuilder<ParameterResource>, IResourceBuilder<ParameterResource>)extensionIResourceBuilder<AzurePostgresFlexibleServerResource>
Configures the resource to use password authentication for Azure PostgreSQL Flexible Server. This overload is used when the PostgreSQL resource is created in a container and the password is stored in an Azure Key Vault secret.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<AzurePostgresFlexibleServerResource> WithPasswordAuthentication(
this IResourceBuilder<AzurePostgresFlexibleServerResource> builder,
IResourceBuilder<IAzureKeyVaultResource> keyVaultBuilder,
IResourceBuilder<ParameterResource>? userName = null,
IResourceBuilder<ParameterResource>? password = null)
{
// ...
}
}
builderIResourceBuilder<AzurePostgresFlexibleServerResource>The Azure PostgreSQL server resource builder.
keyVaultBuilderIResourceBuilder<IAzureKeyVaultResource>The Azure Key Vault resource builder.
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.
IResourceBuilder<AzurePostgresFlexibleServerResource>A reference to the ApplicationModel.IResourceBuilder`1 builder.
WithPostgresMcp(IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource>, Action<IResourceBuilder<PostgresMcpContainerResource>>, string?)Section titled WithPostgresMcp(IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource>, Action<IResourceBuilder<PostgresMcpContainerResource>>, string?)extensionIResourceBuilder<AzurePostgresFlexibleServerDatabaseResource>
Adds a Postgres MCP server container and configures it to connect to the database represented by builder.
public static class AzurePostgresExtensions
{
public static IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource> WithPostgresMcp(
this IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource> builder,
Action<IResourceBuilder<PostgresMcpContainerResource>>? configureContainer = null,
string? containerName = null)
{
// ...
}
}
builderIResourceBuilder<AzurePostgresFlexibleServerDatabaseResource>The Azure Postgres database resource builder.
configureContainerAction<IResourceBuilder<PostgresMcpContainerResource>>optionalConfiguration callback for the Postgres MCP container resource.
containerNamestring?optionalThe name of the container (optional).
IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource>A reference to the ApplicationModel.IResourceBuilder`1.

The Postgres MCP server is configured to use SSE transport and will expose an HTTP endpoint.

This extension method only applies when the Azure PostgreSQL resource is running as a container (i.e., AzurePostgresFlexibleServerDatabaseResource.IsContainer is true). If the resource is not running as a container, this method has no effect.