|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create an Azure Database for PostgreSQL Flexible Server - Azure libraries (SDK) for Python' |
| 3 | +description: In this Quickstart, learn how to create an Azure Database for PostgreSQL Flexible server using Azure libraries (SDK) for Python. |
| 4 | +author: AwdotiaRomanowna |
| 5 | +ms.service: postgresql |
| 6 | +ms.subservice: flexible-server |
| 7 | +ms.custom: devx-track-python-sdk |
| 8 | +ms.topic: quickstart |
| 9 | +ms.author: alkuchar |
| 10 | +ms.date: 04/24/2023 |
| 11 | +--- |
| 12 | + |
| 13 | +# Quickstart: Use an Azure libraries (SDK) for Python to create an Azure Database for PostgreSQL - Flexible Server |
| 14 | + |
| 15 | +[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)] |
| 16 | + |
| 17 | +In this quickstart, you'll learn how to use the [Azure libraries (SDK) for Python](/azure/developer/python/sdk/azure-sdk-overview?view=azure-python&preserve-view=true) |
| 18 | +to create an Azure Database for PostgreSQL - Flexible Server. |
| 19 | + |
| 20 | +Flexible server is a managed service that you use to run, manage, and scale highly available PostgreSQL databases in the cloud. You can use Python SDK to provision a PostgreSQL Flexible Server, multiple servers or multiple databases on a server. |
| 21 | + |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/). |
| 26 | + |
| 27 | +## Create the Server |
| 28 | + |
| 29 | +First, install the required packages. |
| 30 | + |
| 31 | +```bash |
| 32 | +pip install azure-mgmt-resource |
| 33 | +pip install azure-identity |
| 34 | +pip install azure-mgmt-rdbms |
| 35 | +``` |
| 36 | + |
| 37 | +Create a `create_postgres_flexible_server.py` file and include the following code. |
| 38 | + |
| 39 | +```python |
| 40 | +from azure.identity import DefaultAzureCredential |
| 41 | +from azure.mgmt.resource import ResourceManagementClient |
| 42 | +from azure.mgmt.rdbms.postgresql_flexibleservers import PostgreSQLManagementClient |
| 43 | +from azure.mgmt.rdbms.postgresql_flexibleservers.models import Server, Sku, Storage |
| 44 | + |
| 45 | + |
| 46 | +def create_postgres_flexible_server(subscription_id, resource_group, server_name, location): |
| 47 | + # Authenticate with your Azure account |
| 48 | + credential = DefaultAzureCredential() |
| 49 | + |
| 50 | + # Create resource management client and PostgreSQL management client |
| 51 | + resource_client = ResourceManagementClient(credential, subscription_id) |
| 52 | + postgres_client = PostgreSQLManagementClient(credential, subscription_id) |
| 53 | + |
| 54 | + # Create resource group |
| 55 | + resource_client.resource_groups.create_or_update( |
| 56 | + resource_group, |
| 57 | + { |
| 58 | + 'location': location |
| 59 | + } |
| 60 | + ) |
| 61 | + |
| 62 | + # Create PostgreSQL Flexible Server |
| 63 | + server_params = Server( |
| 64 | + sku=Sku(name='Standard_D4s_v3', tier='GeneralPurpose'), |
| 65 | + administrator_login='pgadmin', |
| 66 | + administrator_login_password='<mySecurePassword>', |
| 67 | + storage=Storage(storage_size_gb=32), |
| 68 | + version="14", |
| 69 | + create_mode="Create" |
| 70 | + ) |
| 71 | + |
| 72 | + postgres_client.servers.begin_create( |
| 73 | + resource_group, |
| 74 | + server_name, |
| 75 | + server_params |
| 76 | + ).result() |
| 77 | + |
| 78 | + print(f"PostgreSQL Flexible Server '{server_name}' created in resource group '{resource_group}'") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + subscription_id = '<subscription_id>' |
| 83 | + resource_group = '<resource_group>' |
| 84 | + server_name = '<servername>' |
| 85 | + location = 'eastus' |
| 86 | + |
| 87 | + create_postgres_flexible_server(subscription_id, resource_group, server_name, location) |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | +Replace the following parameters with your data: |
| 92 | + |
| 93 | +- **subscription_id**: Your own [subscription ID](../../azure-portal/get-subscription-tenant-id.md#find-your-azure-subscription). |
| 94 | +- **resource_group**: The name of the resource group you want to use. The script will create a new resource group if it doesn't exist. |
| 95 | +- **server_name**: A unique name that identifies your Azure Database for PostgreSQL server. The domain name `postgres.database.azure.com` is appended to the server name you provide. The server name must be at least 3 characters and at most 63 characters, and can only contain lowercase letters, numbers, and hyphens. |
| 96 | +- **administrator_login**: The primary administrator username for the server. You can create additional users after the server has been created. |
| 97 | +- **administrator_login_password**: A password for the primary administrator for the server. It must contain between 8 and 128 characters. Your password must contain characters from three of the following categories: English uppercase letters, English lowercase letters, numbers (0 through 9), and non-alphanumeric characters (!, $, #, %, etc.). |
| 98 | + |
| 99 | +You can also customize other parameters like location, storage size, engine version, etc. |
| 100 | + |
| 101 | + |
| 102 | +> [!NOTE] |
| 103 | +> Note that the DefaultAzureCredential class will try to authenticate using various methods, such as environment variables, managed identities, or the Azure CLI. |
| 104 | +> Make sure you have one of these methods set up. You can find more information on authentication in the [Azure SDK documentation](/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential&preserve-view=true). |
| 105 | +
|
| 106 | +## Review deployed resources |
| 107 | + |
| 108 | +You can use the Python SDK, Azure portal, Azure CLI, Azure PowerShell, and various other tools to validate the deployment and review the deployed resources. Some examples are provided below. |
| 109 | + |
| 110 | + |
| 111 | +# [Python SDK](#tab/PythonSDK) |
| 112 | +Add the `check_server_created` function to your existing script to use the servers attribute of the [`PostgreSQLManagementClient`](/python/api/azure-mgmt-rdbms/azure.mgmt.rdbms.postgresql_flexibleservers.postgresqlmanagementclient?view=azure-python&preserve-view=true) instance to check if the PostgreSQL Flexible Server was created: |
| 113 | + |
| 114 | +```python |
| 115 | +def check_server_created(subscription_id, resource_group, server_name): |
| 116 | + # Authenticate with your Azure account |
| 117 | + credential = DefaultAzureCredential() |
| 118 | + |
| 119 | + # Create PostgreSQL management client |
| 120 | + postgres_client = PostgreSQLManagementClient(credential, subscription_id) |
| 121 | + |
| 122 | + try: |
| 123 | + server = postgres_client.servers.get(resource_group, server_name) |
| 124 | + if server: |
| 125 | + print(f"Server '{server_name}' exists in resource group '{resource_group}'.") |
| 126 | + print(f"Server state: {server.state}") |
| 127 | + else: |
| 128 | + print(f"Server '{server_name}' not found in resource group '{resource_group}'.") |
| 129 | + except Exception as e: |
| 130 | + print(f"Error occurred: {e}") |
| 131 | + print(f"Server '{server_name}' not found in resource group '{resource_group}'.") |
| 132 | +``` |
| 133 | + |
| 134 | +Call it with the appropriate parameters. |
| 135 | + |
| 136 | +```python |
| 137 | + check_server_created(subscription_id, resource_group, server_name) |
| 138 | +``` |
| 139 | + |
| 140 | +> [!NOTE] |
| 141 | +> The `check_server_created` function will return the server state as soon as the server is provisioned. However, it might take a few minutes for the server to become fully available. Ensure that you wait for the server to be in the Ready state before connecting to it. |
| 142 | +
|
| 143 | + |
| 144 | +# [CLI](#tab/CLI) |
| 145 | + |
| 146 | +```azurecli |
| 147 | +az resource list --resource-group <resource_group> |
| 148 | +``` |
| 149 | + |
| 150 | +# [PowerShell](#tab/PowerShell) |
| 151 | + |
| 152 | +```azurepowershell |
| 153 | +Get-AzResource -ResourceGroupName <resource_group> |
| 154 | +``` |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## Clean up resources |
| 159 | + |
| 160 | +If you no longer need the PostgreSQL Flexible Server, you can delete it and the associated resource group using the following methods. |
| 161 | + |
| 162 | +# [Python SDK](#tab/PythonSDK) |
| 163 | +Add the `delete_resources` function to your existing script to delete your Postgres server and the associated resource group that was created in this quickstart. |
| 164 | + |
| 165 | +```python |
| 166 | +def delete_resources(subscription_id, resource_group, server_name): |
| 167 | + # Authenticate with your Azure account |
| 168 | + credential = DefaultAzureCredential() |
| 169 | + |
| 170 | + # Create resource management client and PostgreSQL management client |
| 171 | + resource_client = ResourceManagementClient(credential, subscription_id) |
| 172 | + postgres_client = PostgreSQLManagementClient(credential, subscription_id) |
| 173 | + |
| 174 | + # Delete PostgreSQL Flexible Server |
| 175 | + postgres_client.servers.begin_delete(resource_group, server_name).result() |
| 176 | + print(f"Deleted PostgreSQL Flexible Server '{server_name}' in resource group '{resource_group}'.") |
| 177 | + |
| 178 | + # Delete resource group |
| 179 | + resource_client.resource_groups.begin_delete(resource_group).result() |
| 180 | + print(f"Deleted resource group '{resource_group}'.") |
| 181 | + |
| 182 | +# Call the delete_resources function |
| 183 | +delete_resources(subscription_id, resource_group, server_name) |
| 184 | +``` |
| 185 | + |
| 186 | + |
| 187 | +# [CLI](#tab/CLI) |
| 188 | + |
| 189 | +```azurecli |
| 190 | +az group delete --name <resource_group> |
| 191 | +``` |
| 192 | + |
| 193 | +# [PowerShell](#tab/PowerShell) |
| 194 | + |
| 195 | +```azurepowershell |
| 196 | +Remove-AzResourceGroup -Name <resource_group> |
| 197 | +``` |
| 198 | + |
| 199 | +--- |
0 commit comments