Skip to main content

Azure Database for PostgreSQL

Prerequisites

The Azure Database for PostgreSQL is a manage database hosted on Azure, which can then be used as the L7|ESP backend database

  • Azure Subscription

CLI tools

The az command line tools will be used to programmatically access/manage the PostgreSQL instance and the Azure subscription. To install the CLI tool, follow the link below and choose the correct operating sytem:

ARM template

The latest ARM template is as follows:

Bicep

param administratorLogin string

@secure()
param administratorLoginPassword string
param location string = resourceGroup().location
param serverName string
param serverEdition string = 'GeneralPurpose'
param skuSizeGB int = 128
param dbInstanceType string = 'Standard_D4ds_v4'
param haMode string = 'ZoneRedundant'
param availabilityZone string = '1'
param version string = '12'
param virtualNetworkExternalId string = ''
param subnetName string = ''
param privateDnsZoneArmResourceId string = ''

resource serverName_resource 'Microsoft.DBforPostgreSQL/flexibleServers@2021-06-01' = {
  name: serverName
  location: location
  sku: {
    name: dbInstanceType
    tier: serverEdition
  }
  properties: {
    version: version
    administratorLogin: administratorLogin
    administratorLoginPassword: administratorLoginPassword
    network: {
      delegatedSubnetResourceId: (empty(virtualNetworkExternalId) ? json('null') : json('${virtualNetworkExternalId}/subnets/${subnetName}'))
      privateDnsZoneArmResourceId: (empty(virtualNetworkExternalId) ? json('null') : privateDnsZoneArmResourceId)
    }
    highAvailability: {
      mode: haMode
    }
    storage: {
      storageSizeGB: skuSizeGB
    }
    backup: {
      backupRetentionDays: 7
      geoRedundantBackup: 'Disabled'
    }
    availabilityZone: availabilityZone
  }
}

JSON

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "administratorLogin": {
      "type": "string",
    },
    "administratorLoginPassword": {
      "type": "secureString"
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]"
    },
    "serverName": {
      "type": "string"
    },
    "serverEdition": {
      "type": "string",
      "defaultValue": "GeneralPurpose"
    },
    "skuSizeGB": {
      "type": "int",
      "defaultValue": 128
    },
    "dbInstanceType": {
      "type": "string",
      "defaultValue": "Standard_D4ds_v4"
    },
    "haMode": {
      "type": "string",
      "defaultValue": "ZoneRedundant"
    },
    "availabilityZone": {
      "type": "string",
      "defaultValue": "1"
    },
    "version": {
      "type": "string",
      "defaultValue": "12"
    },
    "virtualNetworkExternalId": {
      "type": "string",
      "defaultValue": ""
    },
    "subnetName": {
      "type": "string",
      "defaultValue": ""
    },
    "privateDnsZoneArmResourceId": {
      "type": "string",
      "defaultValue": ""
    }
  },
  "resources": [
    {
      "type": "Microsoft.DBforPostgreSQL/flexibleServers",
      "apiVersion": "2021-06-01",
      "name": "[parameters('serverName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('dbInstanceType')]",
        "tier": "[parameters('serverEdition')]"
      },
      "properties": {
        "version": "[parameters('version')]",
        "administratorLogin": "[parameters('administratorLogin')]",
        "administratorLoginPassword": "[parameters('administratorLoginPassword')]",
        "network": {
          "delegatedSubnetResourceId": "[if(empty(parameters('virtualNetworkExternalId')), json('null'), json(format('{0}/subnets/{1}', parameters('virtualNetworkExternalId'), parameters('subnetName'))))]",
          "privateDnsZoneArmResourceId": "[if(empty(parameters('virtualNetworkExternalId')), json('null'), parameters('privateDnsZoneArmResourceId'))]"
        },
        "highAvailability": {
          "mode": "[parameters('haMode')]"
        },
        "storage": {
          "storageSizeGB": "[parameters('skuSizeGB')]"
        },
        "backup": {
          "backupRetentionDays": 7,
          "geoRedundantBackup": "Disabled"
        },
        "availabilityZone": "[parameters('availabilityZone')]"
      }
    }
  ]
}

ARM template parameters

When deploying the ARM template, you can override parameters like so:

  • administratorLogin: The default administrator username.

  • administratorLoginPassword: The default administrator password

  • location: The location of AKS resource. Default value: same region/location as the resource group you are deploying ARM template into.

  • serverName: The name of the server.

  • serverEdition: The edition of the server.

  • skuSizeGB: the storage size, in Gigabyte, of the Azure Database for PostgreSQL server.

  • dbInstanceType: The size of the virtual machine. Default: Standard_D4_v4

  • haMode: high availability mode.

  • availabilityZone: availability of the database.

  • version: the version of PostgreSQL server to deploy.

Deploy ARM template

$serverName = Read-Host -Prompt "Enter a name for the new Azure Database for PostgreSQL server"
$resourceGroupName = Read-Host -Prompt "Enter a name for the new resource group where the server will exist"
$location = Read-Host -Prompt "Enter an Azure region (for example, centralus) for the resource group"
$adminUser = Read-Host -Prompt "Enter the Azure Database for PostgreSQL server's administrator account name"
$adminPassword = Read-Host -Prompt "Enter the administrator password" -AsSecureString

New-AzResourceGroup -Name $resourceGroupName -Location $location # Use this command when you need to create a new resource group for your deployment
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName `
    -TemplateFile "postgres-flexible-server-template.json" `
    -serverName $serverName `
    -administratorLogin $adminUser `
    -administratorLoginPassword $adminPassword

Read-Host -Prompt "Press [ENTER] to continue ..."

Review Deployed resources

Follow these steps to verify if your server was created in Azure.

echo "Enter your Azure Database for PostgreSQL Flexible Server name:" &&
read serverName &&
echo "Enter the resource group where the Azure Database for PostgreSQL Flexible Server exists:" &&
read resourcegroupName &&
az resource show --resource-group $resourcegroupName --name $serverName --resource-type "Microsoft.DBforPostgreSQL/flexibleServers"

Connect ESP instance to Azure PostgreSQL

Once you validate that the postgres database have been successfully created on the Azure portal, gather the database connection strings. You can find this under the connection strings settings of the database on the portal.

3797516441.png

Next, we can add the database connection string on the esp helm chart. on the values.yml file under the database section add the connection string informations:

database: |
    {
    "host": "${POSTGRES_HOST}",
    "port": ${POSTGRES_PORT},
    "name": "${POSTGRES_DBNAME}",
    "user": "${POSTGRES_USER}",
    "pass": "${POSTGRES_PASSWORD}",
    "schema": "${POSTGRES_SCHEMA}",
    "start_service": false
    }

Now deploy the helm chart (see helm documentation). Verify that esp is up and the database schema was successfully created

3797549466.png