Azure RDBMS Management

10.1.1 · active · verified Thu Apr 09

The `azure-mgmt-rdbms` library provides Python client APIs for managing Azure Relational Database services (PostgreSQL, MySQL, MariaDB). It allows for programmatic creation, configuration, and deletion of database servers, databases, and related resources. The current version is `10.1.1` and it follows the Azure SDK release cadence, with frequent updates to align with new Azure API features and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to authenticate with Azure using `DefaultAzureCredential` and then list all Azure PostgreSQL servers within a specified subscription. It requires `AZURE_SUBSCRIPTION_ID` to be set as an environment variable or replaced in the code, and appropriate Azure credentials (e.g., via `az login` or service principal environment variables) for authentication.

import os
from azure.identity import DefaultAzureCredential
from azure.mgmt.rdbms.postgresql import PostgreSQLManagementClient

# Set your Azure Subscription ID and Resource Group name as environment variables
# or replace with actual values. Ensure AZURE_TENANT_ID, AZURE_CLIENT_ID,
# AZURE_CLIENT_SECRET are also set for service principal authentication,
# or be logged in via 'az login' for user authentication.
SUBSCRIPTION_ID = os.environ.get('AZURE_SUBSCRIPTION_ID', 'YOUR_SUBSCRIPTION_ID')

if SUBSCRIPTION_ID == 'YOUR_SUBSCRIPTION_ID':
    print("Warning: Please set the AZURE_SUBSCRIPTION_ID environment variable or replace 'YOUR_SUBSCRIPTION_ID'.")
    exit()

def list_postgresql_servers():
    try:
        # Authenticate with Azure
        credential = DefaultAzureCredential()
        
        # Create a PostgreSQL management client
        pg_client = PostgreSQLManagementClient(credential, SUBSCRIPTION_ID)
        
        print(f"Listing PostgreSQL servers in subscription {SUBSCRIPTION_ID}:")
        for server in pg_client.servers.list():
            print(f"  - Name: {server.name}, Resource Group: {server.id.split('/resourceGroups/')[1].split('/')[0]}, Location: {server.location}, State: {server.user_visible_state}")
            
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    list_postgresql_servers()

view raw JSON →