|
| 1 | +--- |
| 2 | +title: Create and manage Azure Database for PostgreSQL - Flexible Server with data encrypted by Customer Managed Keys using the Azure CLI |
| 3 | +description: Create and manage Azure Database for PostgreSQL - Flexible Server with data encrypted by Customer Managed Keys using the Azure CLI |
| 4 | +author: gennadNY |
| 5 | +ms.author: gennadyk |
| 6 | +ms.service: postgresql |
| 7 | +ms.subservice: flexible-server |
| 8 | +ms.topic: how-to |
| 9 | +ms.date: 12/10/2022 |
| 10 | +--- |
| 11 | +# Create and manage Azure Database for PostgreSQL - Flexible Server with data encrypted by Customer Managed Keys (CMK) using the Azure CLI |
| 12 | + |
| 13 | +[!INCLUDE [applies-to-postgresql-flexible-server](../includes/applies-to-postgresql-flexible-server.md)] |
| 14 | + |
| 15 | +> [!NOTE] |
| 16 | +> CLI examples below are based on 2.43.0 version of Azure Database for PostgreSQL - Flexible Server CLI libraries, which are in preview and may be subject to changes. |
| 17 | +
|
| 18 | +In this article, you learn how to create and manage Azure Database for PostgreSQL - Flexible Server with data encrypted by Customer Managed Keys using the Azure CLI. To learn more about Customer Managed Keys (CMK) feature with Azure Database for PostgreSQL - Flexible Server, see the [overview](concepts-data-encryption.md). |
| 19 | + |
| 20 | +## Setup Customer Managed Key during Server Creation |
| 21 | + |
| 22 | +Prerequisites: |
| 23 | + |
| 24 | +- You must have an Azure subscription and be an administrator on that subscription. |
| 25 | + |
| 26 | +Follow the steps below to enable CMK while creating Postgres Flexible Server using Azure CLI. |
| 27 | + |
| 28 | +1. Create a key vault and a key to use for a customer-managed key. Also enable purge protection and soft delete on the key vault. |
| 29 | + |
| 30 | +```azurecli-interactive |
| 31 | + az keyvault create -g <resource_group> -n <vault_name> --location <azure_region> --enable-purge-protection true |
| 32 | +``` |
| 33 | + |
| 34 | +2. In the created Azure Key Vault, create the key that will be used for the data encryption of the Azure Database for PostgreSQL - Flexible server. |
| 35 | + |
| 36 | +```azurecli-interactive |
| 37 | + keyIdentifier=$(az keyvault key create --name <key_name> -p software --vault-name <vault_name> --query key.kid -o tsv) |
| 38 | +``` |
| 39 | +3. Create Managed Identity which will be used to retrieve key from Azure Key Vault |
| 40 | +```azurecli-interactive |
| 41 | + identityPrincipalId=$(az identity create -g <resource_group> --name <identity_name> --location <azure_region> --query principalId -o tsv) |
| 42 | +``` |
| 43 | + |
| 44 | +4. Add access policy with key permissions of *wrapKey*,*unwrapKey*, *get*, *list* in Azure KeyVault to the managed identity we created above |
| 45 | +```azurecli-interactive |
| 46 | +az keyvault set-policy -g <resource_group> -n <vault_name> --object-id $identityPrincipalId --key-permissions wrapKey unwrapKey get list |
| 47 | +``` |
| 48 | +5. Finally, lets create Azure Database for PostgreSQL - Flexible Server with CMK based encryption enabled |
| 49 | +```azurecli-interactive |
| 50 | +az postgres flexible-server create -g <resource_group> -n <postgres_server_name> --location <azure_region> --key $keyIdentifier --identity <identity_name> |
| 51 | +``` |
| 52 | +## Update Customer Managed Key on the CMK enabled Flexible Server |
| 53 | + |
| 54 | +Prerequisites: |
| 55 | +- You must have an Azure subscription and be an administrator on that subscription. |
| 56 | +- Key Vault with key in region where Postgres Flex Server will be created. Follow this [tutorial](../../key-vault/general/quick-create-portal.md) to create Key Vault and generate key. |
| 57 | + |
| 58 | +Follow the steps below to change\rotate key or identity after creation of server with data encryption. |
| 59 | +1. Change key/identity for data encryption for existing server, first lets get new key identifier |
| 60 | +```azurecli-interactive |
| 61 | + newKeyIdentifier=$(az keyvault key show --vault-name <vault_name> --name <key_name> --query key.kid -o tsv) |
| 62 | +``` |
| 63 | +2. Update server with new key and\or identity |
| 64 | +```azurecli-interactive |
| 65 | + az postgres flexible-server update --resource-group <resource_group> --name <server_name> --key $newKeyIdentifier --identity <identity_name> |
| 66 | +``` |
| 67 | +## Next steps |
| 68 | + |
| 69 | +- [Manage an Azure Database for PostgreSQL - Flexible Server by using the Azure CLI](how-to-manage-server-cli.md) |
0 commit comments