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:
|Select Try It in the upper-right corner of a code block. Selecting Try It doesn't automatically copy the code to Cloud Shell.|
|Go to https://shell.azure.com, or select the Launch Cloud Shell button to open Cloud Shell in your browser.|
|Select the Cloud Shell button on the menu bar at the upper right in the Azure portal.|
To run the code in this article in Azure Cloud Shell:
Start Cloud Shell.
Select the Copy button on a code block to copy the code.
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.
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.
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
This script uses the commands outlined in the following table:
|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.|
- Read more information on the Azure CLI: Azure CLI documentation.
- Try additional scripts: Azure CLI samples for Azure Database for PostgreSQL
- How to backup and restore a server in Azure Database for PostgreSQL using the Azure portal