Skip to content
Docs Try Aspire

AzurePostgresExtensions Methods

Class Methods 8 members
Provides extension methods for adding the Azure PostgreSQL resources to the application model.
AddAzurePostgresFlexibleServer(IDistributedApplicationBuilder, string) Section titled AddAzurePostgresFlexibleServer(IDistributedApplicationBuilder, string) extension IResourceBuilder<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)
{
// ...
}
}
builder IDistributedApplicationBuilder The builder for the distributed application.
name string The 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?) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<AzurePostgresFlexibleServerResource> The Azure PostgreSQL server resource builder.
name string The name of the resource. This name will be used as the connection string name when referenced in a dependency.
databaseName string? optional The 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>) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<PostgresServerResource> The ApplicationModel.IResourceBuilder`1 builder.
IResourceBuilder<PostgresServerResource> A reference to the ApplicationModel.IResourceBuilder`1 builder.
PublishAsAzurePostgresFlexibleServer(IResourceBuilder<PostgresServerResource>) Section titled PublishAsAzurePostgresFlexibleServer(IResourceBuilder<PostgresServerResource>) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<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>>) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<AzurePostgresFlexibleServerResource> The Azure PostgreSQL server resource builder.
configureContainer Action<IResourceBuilder<PostgresServerResource>> optional Callback 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>) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<AzurePostgresFlexibleServerResource> The Azure PostgreSQL server resource builder.
userName IResourceBuilder<ParameterResource> optional The parameter used to provide the user name for the PostgreSQL resource. If null a default value will be used.
password IResourceBuilder<ParameterResource> optional The 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>) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<AzurePostgresFlexibleServerResource> The Azure PostgreSQL server resource builder.
keyVaultBuilder IResourceBuilder<IAzureKeyVaultResource> The Azure Key Vault resource builder.
userName IResourceBuilder<ParameterResource> optional The parameter used to provide the user name for the PostgreSQL resource. If null a default value will be used.
password IResourceBuilder<ParameterResource> optional The 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?) extension IResourceBuilder<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)
{
// ...
}
}
builder IResourceBuilder<AzurePostgresFlexibleServerDatabaseResource> The Azure Postgres database resource builder.
configureContainer Action<IResourceBuilder<PostgresMcpContainerResource>> optional Configuration callback for the Postgres MCP container resource.
containerName string? optional The 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.