Restore an Azure Database for PostgreSQL server using Azure CLI

This sample CLI script restores a single Azure Database for PostgreSQL server to a previous point in time.

Use Azure Cloud Shell

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

To start 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 https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser. Launch Cloud Shell in a new window
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. Start Cloud Shell.

  2. Select the Copy button on a code block to copy the code.

  3. Paste the code into the Cloud Shell session by selecting Ctrl+Shift+V on Windows and Linux or by selecting Cmd+Shift+V on macOS.

  4. Select 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 subscription ID used in the az monitor commands with your own subscription ID.

#!/bin/bash

# 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 \

# Restore a server from backup to a new server
az postgres server restore \
--name mydemoserver-restored \
--resource-group myresourcegroup \
--restore-point-in-time "2018-02-11T13:10:00Z" \
--source-server mydemoserver

Clean up deployment

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

#!/bin/bash
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 postgresql server create Creates a PostgreSQL server that hosts the databases.
az postgresql server restore Restore a server from backup.
az group delete Deletes a resource group including all nested resources.

Next steps