|
| 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 a 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 an [Azure libraries (SDK) for Python](https://learn.microsoft.com/azure/developer/python/sdk/azure-sdk-overview?view=azure-python) |
| 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 | +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_server = 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 placeholders with your data: |
| 92 | + |
| 93 | +- **<subscription_id>**: |
| 94 | +- **<resource_group>**: enter your own login account to use when you connect to the server. For example, `myadmin`. |
| 95 | +- **<servername>**: 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. Server name must be at least 3 characters and at most 63 characters. Server name must 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 | +- **<mySecurePassword>**: 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 change other parameters like location, storage size, engine version etc. |
| 100 | + |
| 101 | +> [!NOTE] |
| 102 | +> Note that the DefaultAzureCredential class will try to authenticate using various methods, such as environment variables, managed identities, or the Azure CLI. |
| 103 | +> Make sure you have one of these methods set up. You can find more information on authentication in the [Azure SDK documentation](https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential). |
| 104 | +
|
| 105 | +## Review deployed resources |
| 106 | + |
| 107 | +Use the Azure portal, Azure CLI, or Azure PowerShell to validate the deployment and review the deployed resources. |
| 108 | + |
| 109 | +# [CLI](#tab/CLI) |
| 110 | + |
| 111 | +```azurecli |
| 112 | +az resource list --resource-group <resource_group> |
| 113 | +``` |
| 114 | + |
| 115 | +# [PowerShell](#tab/PowerShell) |
| 116 | + |
| 117 | +```azurepowershell |
| 118 | +Get-AzResource -ResourceGroupName <resource_group> |
| 119 | +``` |
| 120 | + |
| 121 | +--- |
| 122 | + |
| 123 | +## Clean up resources |
| 124 | + |
| 125 | +Keep this resource group, server, and single database if you want to go to the [Next steps](#next-steps). |
| 126 | +The next steps show you how to connect and query your database using different methods. |
| 127 | + |
| 128 | +To delete the resource group: |
| 129 | + |
| 130 | +# [CLI](#tab/CLI) |
| 131 | + |
| 132 | +```azurecli |
| 133 | +az group delete --name <resource_group> |
| 134 | +``` |
| 135 | + |
| 136 | +# [PowerShell](#tab/PowerShell) |
| 137 | + |
| 138 | +```azurepowershell |
| 139 | +Remove-AzResourceGroup -Name exampleRG |
| 140 | +``` |
| 141 | + |
| 142 | +--- |
0 commit comments