Azure PostgreSQL Flexible Servers Management Client Library
The `azure-mgmt-postgresqlflexibleservers` library is the Microsoft Azure Postgresqlflexibleservers Management Client Library for Python. It enables developers to programmatically create, configure, manage, and scale Azure Database for PostgreSQL flexible server instances. This includes operations like managing databases, configuring firewall rules, performing scaling, and handling backup and restore. The library is currently at version 2.0.0 and is actively maintained, receiving regular updates often in conjunction with Azure service updates.
Warnings
- gotcha Authentication with `DefaultAzureCredential` relies on specific environment variables (e.g., AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID) or an active Azure CLI login. Incorrect or missing configuration is a frequent source of authentication errors.
- gotcha Deployment of new Azure Database for PostgreSQL Flexible Servers can fail due to subscription-level quotas being exceeded or the selected region not having capacity or access enabled for your subscription. This often results in 'Exceeded quota' or 'Enable region' errors.
- gotcha Network access must be properly configured for your PostgreSQL flexible server. If using public access, you must add your client workstation's IP address to the server's firewall rules. If using private access, your client must be within the same virtual network as the server.
- gotcha The library provides both synchronous (`azure.mgmt.postgresqlflexibleservers`) and asynchronous (`azure.mgmt.postgresqlflexibleservers.aio`) clients. Mixing them without proper `await`/`async` handling in Python can lead to runtime errors or unexpected behavior. Ensure consistency in your usage.
- gotcha In-place major version upgrades of PostgreSQL instances performed via the management plane (exposed by this library) are irreversible. While they simplify the upgrade process, there is no automated rollback to the previous major version. Point-in-time recovery to before the upgrade is the only way to revert.
Install
-
pip install azure-mgmt-postgresqlflexibleservers azure-identity
Imports
- PostgreSQLManagementClient
from azure.mgmt.postgresqlflexibleservers import PostgreSQLManagementClient
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.postgresqlflexibleservers import PostgreSQLManagementClient
# Set environment variables or ensure Azure CLI is logged in
# AZURE_SUBSCRIPTION_ID
# AZURE_RESOURCE_GROUP
# AZURE_LOCATION
# AZURE_SERVER_NAME
# AZURE_ADMIN_USERNAME
# AZURE_ADMIN_PASSWORD
subscription_id = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')
resource_group_name = os.environ.get('AZURE_RESOURCE_GROUP', 'my-postgresql-rg')
server_name = os.environ.get('AZURE_SERVER_NAME', 'my-flex-server-123')
location = os.environ.get('AZURE_LOCATION', 'eastus') # e.g., 'eastus'
admin_username = os.environ.get('AZURE_ADMIN_USERNAME', 'psqladmin')
admin_password = os.environ.get('AZURE_ADMIN_PASSWORD', 'ComplexP@ssw0rd')
# Authenticate using DefaultAzureCredential
credential = DefaultAzureCredential()
# Create a PostgreSQLManagementClient
client = PostgreSQLManagementClient(credential, subscription_id)
print(f"Creating PostgreSQL flexible server '{server_name}' in resource group '{resource_group_name}'...")
# Create a flexible server
# This is an asynchronous operation, use .begin_create() and wait() for completion
server_poller = client.servers.begin_create(
resource_group_name,
server_name,
{
"location": location,
"sku": {
"name": "Standard_D2ds_v4",
"tier": "GeneralPurpose"
},
"properties": {
"version": "14", # or "12", "13", "15", etc.
"administratorLogin": admin_username,
"administratorLoginPassword": admin_password,
"storage": {
"storageSizeGB": 32
},
"network": {
"publicNetworkAccess": "Enabled"
}
}
}
)
server = server_poller.result() # Wait for the creation to complete
print(f"Server '{server.name}' created successfully in {server.location}.")
print(f"Provisioning state: {server.provisioning_state}")
# Example: List all servers in the resource group
print(f"\nListing servers in resource group '{resource_group_name}':")
for s in client.servers.list_by_resource_group(resource_group_name):
print(f"- {s.name} ({s.location}) - State: {s.state}")
# Note: This quickstart creates a resource. Remember to delete it after use
# client.servers.begin_delete(resource_group_name, server_name).wait()
# print(f"Server '{server_name}' deleted.")