Tutorial: Design an Azure Database for PostgreSQL - Single Server using Azure CLI

APPLIES TO: Azure Database for PostgreSQL - Single Server

In this tutorial, you use Azure CLI (command-line interface) and other utilities to learn how to:

  • Create an Azure Database for PostgreSQL server
  • Configure the server firewall
  • Use psql utility to create a database
  • Load sample data
  • Query data
  • Update data
  • Restore data

If you don't have an Azure subscription, create an Azure free account before you begin.


Launch Azure Cloud Shell

The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. It has common Azure tools preinstalled and configured to use with your account.

To open the Cloud Shell, just select Try it from the upper right corner of a code block. You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com.

When Cloud Shell opens, verify that Bash is selected for your environment. Subsequent sessions will use Azure CLI in a Bash environment, Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press Enter to run it.

Sign in to Azure

Cloud Shell is automatically authenticated under the initial account signed-in with. Use the following script to sign in using a different subscription, replacing <Subscription ID> with your Azure Subscription ID. If you don't have an Azure subscription, create an Azure free account before you begin.

subscription="<subscriptionId>" # add subscription here

az account set -s $subscription # ...or use 'az login'

For more information, see set active subscription or log in interactively

Set parameter values

The following values are used in subsequent commands to create the database and required resources. Server names need to be globally unique across all of Azure so the $RANDOM function is used to create the server name.

Change the location as appropriate for your environment. Replace with the IP address range to match your specific environment. Use the public IP address of the computer you're using to restrict access to the server to only your IP address.

# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="East US"
# Specify appropriate IP address values for your environment
# to limit / allow access to the PostgreSQL server
echo "Using resource group $resourceGroup with login: $login, password: $password..."

Create a resource group

Create a resource group with the az group create command. An Azure resource group is a logical container into which Azure resources are deployed and managed. The following example creates a resource group named myResourceGroup in the eastus location:

# Create a resource group
echo "Creating $resourceGroup in $location..."
az group create --name $resourceGroup --location "$location" --tags $tag

Create a server

Create a server with the az postgres server create command.

# 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.
echo "Creating $server in $location..."
az postgres server create --name $server --resource-group $resourceGroup --location "$location" --admin-user $login --admin-password $password --sku-name $sku


  • The server name can contain only lowercase letters, numbers, and the hyphen (-) character. It must contain 3 to 63 characters. For more information, see Azure Database for PostgreSQL Naming Rules.
  • The user name for the admin user can't be azure_superuser, admin, administrator, root, guest, or public.
  • The password must contain 8 to 128 characters from three of the following categories: English uppercase letters, English lowercase letters, numbers, and non-alphanumeric characters.
  • For information about SKUs, see Azure Database for PostgreSQL pricing.


Configure a server-based firewall rule

Create a firewall rule with the az postgres server firewall-rule create command to give your local environment access to connect to the server.

# Configure a firewall rule for the server 
echo "Configuring a firewall rule for $server for the IP address range of $startIp to $endIp"
az postgres server firewall-rule create --resource-group $resourceGroup --server $server --name AllowIps --start-ip-address $startIp --end-ip-address $endIp


If you don't know your IP address, go to WhatIsMyIPAddress.com to get it.


To avoid connectivity issues, make sure your network's firewall allows port 5432. Azure Database for PostgreSQL servers use that port.

List server-based firewall rules

To list the existing server firewall rules, run the az postgres server firewall-rule list command.

# List firewall rules for the server
echo "List of server-based firewall rules for $server"
az postgres server firewall-rule list --resource-group $resourceGroup --server-name $server
# You may use the switch `--output table` for a more readable table format as the output.

The output lists the firewall rules, if any, by default in JSON format. You may use the switch --output table for a more readable table format as the output.

Get the connection information

To connect to your server, provide host information and access credentials.

az postgres server show --resource-group $resourceGroup --name $server

Make a note of the administratorLogin and fullyQualifiedDomainName values.

Connect to the Azure Database for PostgreSQL server by using psql

The psql client is a popular choice for connecting to PostgreSQL servers. You can connect to your server by using psql with Azure Cloud Shell. You can also use psql on your local environment if you have it available. An empty database, postgres, is automatically created with a new PostgreSQL server. You can use that database to connect with psql, as shown in the following code.

psql --host=<server_name>.postgres.database.azure.com --port=5432 --username=<admin_user>@<server_name> --dbname=postgres


If you prefer to use a URL path to connect to Postgres, URL encode the @ sign in the username with %40. For example, the connection string for psql would be:

psql postgresql://<admin_user>%40<server_name>@<server_name>.postgres.database.azure.com:5432/postgres

Create a blank database

  1. Once you are connected to the server, create a blank database at the prompt:

    CREATE DATABASE mypgsqldb;
  2. At the prompt, execute the following command to switch connection to the newly created database mypgsqldb:

    \c mypgsqldb

Create tables in the database

Now that you know how to connect to the Azure Database for PostgreSQL, you can complete some basic tasks:

First, create a table and load it with some data. For example, create a table that tracks inventory information:

CREATE TABLE inventory (
  id serial PRIMARY KEY, 
  name VARCHAR(50), 
  quantity INTEGER

You can see the newly created table in the list of tables now by typing:


Load data into the table

Now that there is a table created, insert some data into it. At the open command prompt window, run the following query to insert some rows of data:

INSERT INTO inventory (id, name, quantity) VALUES (1, 'banana', 150); 
INSERT INTO inventory (id, name, quantity) VALUES (2, 'orange', 154);

You have now added two rows of sample data into the table you created earlier.

Query and update the data in the tables

Execute the following query to retrieve information from the inventory table:

SELECT * FROM inventory;

You can also update the data in the inventory table:

UPDATE inventory SET quantity = 200 WHERE name = 'banana';

You can see the updated values when you retrieve the data:

SELECT * FROM inventory;

Restore a database to a previous point in time

Imagine you have accidentally deleted a table. This is something you cannot easily recover from. Azure Database for PostgreSQL allows you to go back to any point-in-time for which your server has backups (determined by the backup retention period you configured) and restore this point-in-time to a new server. You can use this new server to recover your deleted data.

The following command restores the sample server to a point before the table was added:

az postgres server restore --resource-group myresourcegroup --name mydemoserver-restored --restore-point-in-time 2017-04-13T13:59:00Z --source-server mydemoserver

The az postgres server restore command needs the following parameters:

Setting Suggested value Description  
resource-group  myresourcegroup  The resource group in which the source server exists. 
name mydemoserver-restored The name of the new server that is created by the restore command.
restore-point-in-time 2017-04-13T13:59:00Z Select a point-in-time to restore to. This date and time must be within the source server's backup retention period. Use ISO8601 date and time format. For example, you may use your own local timezone, such as 2017-04-13T05:59:00-08:00, or use UTC Zulu format 2017-04-13T13:59:00Z.
source-server mydemoserver The name or ID of the source server to restore from.

Restoring a server to a point-in-time creates a new server, copied as the original server as of the point in time you specify. The location and pricing tier values for the restored server are the same as the source server.

The command is synchronous, and will return after the server is restored. Once the restore finishes, locate the new server that was created. Verify the data was restored as expected.

Clean up resources

Use the following command to remove the resource group and all resources associated with it using the az group delete command - unless you have an ongoing need for these resources. Some of these resources may take a while to create, as well as to delete.

az group delete --name $resourceGroup

Next steps

In this tutorial, you learned how to use Azure CLI (command-line interface) and other utilities to:

  • Create an Azure Database for PostgreSQL server
  • Configure the server firewall
  • Use the psql utility to create a database
  • Load sample data
  • Query data
  • Update data
  • Restore data