Blog Post

Azure Database for PostgreSQL Blog
4 MIN READ

Create a master server and read replicas in Azure Database for PostgreSQL using the Azure CLI

ramkychan's avatar
ramkychan
Icon for Microsoft rankMicrosoft
Jun 14, 2019

With read replicas in Azure Database for PostgreSQL, you can scale out read-heavy workloads and increase the read throughput of your application. Read replicas allow you to replicate data from a master server to up to five read-only replica servers. Replica servers are updated asynchronously using the PostgreSQL engine’s native replication technology. 

 

You can view some of the application patterns and reference architectures that our customers and partners have implemented using read replicas to scale out their workload here.

 

The bash script below allows you to create the master server and read replicas in a single pass. You can run the script from a VM, after confirming that you have installed the latest version of the Azure CLI. Alternatively, you can use the Azure Cloud Shell which always has the latest Azure CLI.

 

For the latest version of the script, visit our GitHub repo: https://github.com/Azure/azure-postgresql/blob/master/samples/scripts/pgreplica.sh

 

#!/bin/bash
#
# PURPOSE
# Create Azure Database for PostgreSQL Server Master and Replica Servers
#
# DESCRIPTION
# This script 
# 1. Creates a Master Server
# 2. Sets the azure.replication_support parameter to REPLICA on the master server
# 3. Restarts the master server for the parameter change to take effect 
# 4. Creates Read Replicas
#
# PREREQUISITES
# Azure CLI (https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest)
# 
# NOTE
# Please modify the script to specify different options for master server creation
# https://docs.microsoft.com/en-us/cli/azure/postgres/server?view=azure-cli-latest
# 
# USAGE
# bash pgreplica.sh
#         --subscription-id 'ffffffff-ffff-ffff-ffff-ffffffffffff'
#         --resource-group 'replicaRG'
#         --region 'northeurope'
#         --server-name 'mypg'
#         --admin-user 'azureuser'
#         --admin-password 'P@ssw0rd1'
#         --sku-name 'GP_Gen5_2'
#         --number-of-replicas '5'
#

function usage()
{
    echo ""
    echo "PURPOSE"
    echo "Create Azure Database for PostgreSQL Server Master and Replica Servers"
    echo ""
    echo "DESCRIPTION"
    echo "This script "
    echo " 1. Creates a Master Server"
    echo " 2. Sets the azure.replication_support parameter to REPLICA on the master server"
    echo " 3. Restarts the master server for the parameter change to take effect"
    echo " 4. Creates Read Replicas"
    echo ""
    echo "PREREQUISITES"
    echo "Azure CLI (https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest)"
    echo ""
    echo "NOTE"
    echo "Please modify the script to specify different options for master server creation"
    echo "https://docs.microsoft.com/en-us/cli/azure/postgres/server?view=azure-cli-latest"
    echo ""
    echo "USAGE"
    echo "bash pgreplica.sh"
    echo -e "\t--subscription-id 'ffffffff-ffff-ffff-ffff-ffffffffffff'"
    echo -e "\t--resource-group 'replicaRG'"
    echo -e "\t--region 'northeurope'"
    echo -e "\t--server-name 'mypg'"
    echo -e "\t--admin-user 'azureuser'"
    echo -e "\t--admin-password 'P@ssw0rd1'"
    echo -e "\t--sku-name 'GP_Gen5_2'"
    echo -e "\t--number-of-replicas '5'"
    echo ""
}

PARAMS=""
while (( "$#" )); do
  case "$1" in
    -h | --help)
      usage
      exit
      ;;
    -a|--subscription-id)
      SUBSCRIPTION_ID=$2
      shift 2
      ;;
    -b|--resource-group)
      RESOURCE_GROUP=$2
      shift 2
      ;;
    -c|--region)
      REGION=$2
      shift 2
      ;;
    -d|--server-name)
      SERVER_NAME=$2
      shift 2
      ;;
    -e|--admin-user)
      ADMIN_USER=$2
      shift 2
      ;;
    -f|--admin-password)
      ADMIN_PASSWORD=$2
      shift 2
      ;;
    -g|--sku-name)
      SKU_NAME=$2
      shift 2
      ;;
    -h|--number-of-replicas)
      NUMBER_OF_REPLICAS=$2
      shift 2
      ;;
    --) # end argument parsing
      shift
      break
      ;;
    -*|--*=) # unsupported flags
      echo "Error: Unsupported flag $1" >&2
      exit 1
      ;;
    *) # preserve positional arguments
      PARAMS="$PARAMS $1"
      shift
      ;;
  esac
done
# set positional arguments in their proper place
eval set -- "$PARAMS"

# Prerequisites - Install Azure CLI
if ! [ -x "$(command -v az)" ]; then
  echo 'Error: azure cli is not installed. Please install from https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest' >&2
  exit 1
fi

# Login
az login

# Set Default Subscription
az account set --subscription "$SUBSCRIPTION_ID"

# Set Default Resource Group
az configure --defaults group="$RESOURCE_GROUP"

# Create Master Server
az postgres server create --location "$REGION" --name "$SERVER_NAME" --admin-user "$ADMIN_USER" --admin-password "$ADMIN_PASSWORD" --sku-name "$SKU_NAME"

# Set azure.replication_support to REPLICA
az postgres server configuration set --name "azure.replication_support" --server-name "$SERVER_NAME" --value "REPLICA"

# Restart Master Server
az postgres server restart --name "$SERVER_NAME"

# Create Replicas
for ((i=1; i<=NUMBER_OF_REPLICAS; i++)); do
  az postgres server replica create --name "$SERVER_NAME-replica-$i" --source-server "$SERVER_NAME"
done

# Logout
azure logout -u "$SUBSCRIPTION_ID"

# Visit Master Server Replication Page
echo "Master and Replicas Creation Successful !!"
echo "Master Server : https://portal.azure.com/#resource/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP/providers/Microsoft.DBforPostgreSQL/servers/$SERVER_NAME/replication"

 

If you have trouble running the script, please let us know by opening an issue here: https://github.com/Azure/azure-postgresql/issues

 

Also, feel free to contribute any updates or bug fixes by creating a pull request: https://github.com/Azure/azure-postgresql/pulls

 

Thank you!

 

Updated Jun 14, 2019
Version 2.0
No CommentsBe the first to comment