Monitor and scale a single PostgreSQL server using Azure CLI

This sample CLI script scales a single Azure Database for PostgreSQL server to a different performance level after querying the metrics.

Use Azure Cloud Shell

Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. Cloud Shell lets you use either bash or PowerShell to work with Azure services. You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

To launch Azure Cloud Shell:

Option Example/Link
Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell. Example of Try It for Azure Cloud Shell
Go to or select the Launch Cloud Shell button to open Cloud Shell in your browser.
Select the Cloud Shell button on the top-right menu bar in the Azure portal. Cloud Shell button in the Azure portal

To run the code in this article in Azure Cloud Shell:

  1. Launch Cloud Shell.
  2. Select the Copy button on a code block to copy the code.
  3. Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. Press Enter to run the code.

If you choose to run the CLI locally, this article requires Azure CLI version 2.0 or later. Check the version by running az --version. See Install Azure CLI to install or upgrade your version of Azure CLI.

Sample script

In this sample script, edit the highlighted lines to update the admin username and password to your own. Replace the SubscriptionID used in the az monitor commands with your own subscription ID.


# Create a resource group
az group create \
--name myresourcegroup \
--location westus

# Create a PostgreSQL server in the resource group
# Name of a server maps to DNS name and is thus required to be globally unique in Azure.
# Substitute the <server_admin_password> with your own value.
az postgres server create \
--name mydemoserver \
--resource-group myresourcegroup \
--location westus \
--admin-user myadmin \
--admin-password <server_admin_password> \
--sku-name GP_Gen4_2 \

# Monitor usage metrics - CPU
az monitor metrics list \
--resource "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforPostgreSQL/servers/mydemoserver" \
--metric cpu_percent \
--interval PT1M

# Monitor usage metrics - Storage
az monitor metrics list \
--resource "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myresourcegroup/providers/Microsoft.DBforPostgreSQL/servers/mydemoserver" \
--metric storage_used \
--interval PT1M

# Scale up the server to provision more vCores within the same Tier
az postgres server update \
--resource-group myresourcegroup \
--name mydemoserver \
--sku-name GP_Gen4_4

# Scale up the server to provision a storage size of 7GB
az postgres server update \
--resource-group myresourcegroup \
--name mydemoserver \
--storage-size 7168

Clean up deployment

Use the following command to remove the resource group and all resources associated with it after the script has been run.

az group delete --name myresourcegroup

Script explanation

This script uses the commands outlined in the following table:

Command Notes
az group create Creates a resource group in which all resources are stored.
az postgres server create Creates a PostgreSQL server that hosts the databases.
az monitor metrics list List the metric value for the resources.
az group delete Deletes a resource group including all nested resources.

Next steps