Postgres Pro Enterprise for Azure

Microsoft Azure VM with Postgres Pro Enterprise Quick Start Guide

Internet access and valid Microsoft Azure account are required to use Postgres Pro 9.6/10/11 database in Microsoft Azure cloud.

Postgres Pro 9.6/10/11 virtual machine (VM) image is available in Microsoft Azure Marketplace.

Software required for installation:

  • Azure CLI 2.0  for cloud management
  • 'psql' or 'Pgadmin' for database connection

Azure CLI 2.0 is a cross-platform command line utility. 

Azure Portal https://portal.azure.com or Azure PowerShell can be used as well.

Azure CLI 2.0 Installation guide: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli

Azure CLI 2.0 Get started guide: https://docs.microsoft.com/en-us/cli/azure/get-started-with-azure-cli

Azure CLI 2.0 Command reference guide: https://docs.microsoft.com/en-us/cli/azure

Azure Linux Virtual Machine Installation guide: https://docs.microsoft.com/en-us/azure/virtual-machines/linux

Azure Backup Documentation: https://docs.microsoft.com/en-us/azure/backup/

 

Connection to Azure and environment check

  • Connect to Microsoft Azure with:
az login

or

az login --username <myusername>
  • Set Azure CLI 2.0 commands table format output:
az configure --output table
  • Verify Azure CLI 2.0 version (should be 2.0.72 or higher):
az --version | head -1
  • Make sure required services Microsoft.Storage, Microsoft.Compute and Microsoft.Network are registered:
az provider show --namespace Microsoft.Storage
az provider show --namespace Microsoft.Compute
az provider show --namespace Microsoft.Network
  • If not, register them:
az provider register --namespace Microsoft.Storage
az provider register --namespace Microsoft.Compute
az provider register --namespace Microsoft.Network
  • A list of locations for VM:
az account list-locations

‘northeurope’ location will be used further.

  • A list of available VM sizes in ‘northeurope’ location:
az vm list-sizes --location northeurope

‘Standard_DS1_v2’ VM size will be used further (available for ‘Free Trial’ subscription).

  • Obtain publisher name of VM Postgres Pro image in Microsoft Azure Marketplace for ‘northeurope’ location:
az vm image list-publishers \
--location northeurope \
--query "[?starts_with(name,'postgres')].{Name:name}"

‘postgres-pro’ VM publisher name will be used further.

  • Obtain VM Postgres Pro image names available in Microsoft Azure Marketplace for ‘postgres-pro’ publisher in ‘northeurope’ location:
az vm image list \
--publisher postgres-pro \
--location northeurope \
--all \
--query "[?contains(urn,'enterprise')].{Urn:urn}"
  • The following VM image name will be used further:
urn_id='postgres-pro:postgres-pro-enterprise:ent-9_6_15-centos-7_6_20190814-hourly:96.15.2'
echo $urn_id
or
urn_id='postgres-pro:postgres-pro-enterprise-10:ent-10_10-centos-7_6_20190815-hourly:10.10.2'
echo $urn_id
or
urn_id='postgres-pro:postgres-pro-enterprise-11:ent-11_5-centos-7_6_20190815-hourly:11.5.4'
echo $urn_id
  • Configure VM programmatic deployment:
az vm image accept-terms --urn $urn_id
  • Create a pair of private/public ssh-keys in ~/.ssh directory to connect to VM:
ssh-keygen -t rsa -b 2048

 

VM creation

  • Create resource group:
az group create \
--name myresourcegroup \
--location northeurope
  • Create VM from VM image available in Microsoft Azure Marketplace: 
az vm create \
--name myvm-ent-xx \
--resource-group myresourcegroup \
--image $urn_id \
--location northeurope \
--size Standard_DS1_v2 \
--ssh-key-value ~/.ssh/id_rsa.pub \
--admin-username azureuser \
--authentication-type ssh \
--public-ip-address-dns-name myvm-ent-xx-dnsname \
--os-disk-name myvm-ent-xx-osdisk

Replace ‘xx’ by '01', '02', '03’ and so on.

 

Connection to VM

As a result VM is created with FQDN of 'myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com' (FQDN is a combination of short DNS-name used during VM creation, location name and ‘cloudapp.azure.com’) and OS-account of ‘azureuser’ (by default with ‘sudo’ permissions)

  • Connect to VM: 
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com

Replace ‘xx’ by '01', '02', '03’ and so on.

 

Postgres Pro database service status

  • Verify Postgres Pro database service status: 
sudo systemctl -l status postgrespro-enterprise-9.6.service
or
sudo systemctl -l status postgrespro-ent-10.service
or
sudo systemctl -l status postgrespro-ent-11.service
  • To stop/start Postgres Pro database service use the following commands: 
sudo systemctl stop postgrespro-enterprise-9.6.service
sudo systemctl start postgrespro-enterprise-9.6.service
or
sudo systemctl stop postgrespro-ent-10.service
sudo systemctl start postgrespro-ent-10.service
or
sudo systemctl stop postgrespro-ent-11.service
sudo systemctl start postgrespro-ent-11.service

 

Connection to Postgres Pro database

  • Switch to ‘postgres’ account:
sudo su - postgres
  • To connect to Postgres Pro database use the following command:
psql
  • To exit from ‘psql’ use the following command:
\q
  • To return to Azure CLI 2.0 interface run 'exit' command twice

 

External connection to VM

  • TCP-port 5433 has to be opened for external connection to Postgres Pro database: 
az vm open-port \
--name myvm-ent-xx \
--port 5433 \
--resource-group myresourcegroup \
--priority 1001
  • TCP-ports 80 and 443 have to be opened for external connection to database monitoring server: 
az vm open-port \
--name myvm-ent-xx \
--port 80 \
--resource-group myresourcegroup \
--priority 1002

az vm open-port \
--name myvm-ent-xx \
--port 443 \
--resource-group myresourcegroup \
--priority 1003

Replace ‘xx’ by '01', '02', '03’ and so on.

 

External connection to Postgres Pro database

  • For external connection to Postgres Pro database set up 'postgres' user password:
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com

sudo su - postgres
psql -c "alter user postgres with encrypted password 'YOUR_POSTGRES_USER_PASSWORD'"
exit

exit
  • For external connection to Postgres Pro database using ‘psql’ utility use the following command:
psql --host=myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com --port=5433 --username=postgres --dbname=postgres
  • For external connection to Postgres Pro database using ‘Pgadmin’ utility configure the following server settings in ‘Pgadmin’ menu:
    • ‘mydb-xx’ for ‘Name’
    • ‘myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com’ for ‘Host’
    • ‘5433’ for ‘Port’
    • ‘postgres’ for ‘Maintenance DB’
    • ‘postgres’ for ‘Username’

Replace ‘xx’ by '01', '02', '03’ and so on.

 

External connection to database monitoring server

  • For connection to database monitoring server set up 'Admin' user password:
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com

sudo su - postgres
source .pgsql_profile
psql --dbname=zabbix --username=zabbix -c "update users set passwd=md5('YOUR_ZABBIX_ADMIN_PASSWORD') where alias='Admin'"
exit

exit
  • External connection to database monitoring server is via the following link:

https://myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com/zabbix

(Replace ‘xx’ by '01', '02', '03’ and so on.)

 

VM configuration change

Let's have some examples of VM configuration change:

1) Change VM size from ‘Standard_DS1_v2’ to ‘Standard_DS2_v2’ to increase VM computing power

  • $PGDATA/postgresql.tune.lock file can be deleted in order to automatically adjust Postgres Pro database parameters values before increasing VM size:
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com

sudo su - postgres
cp $PGDATA/postgresql.auto.conf $PGDATA/postgresql.auto.conf.ORIG
rm $PGDATA/postgresql.tune.lock
exit

exit
  • A list of available VM sizes in ‘northeurope’ location (‘az vm deallocate’ is not required):
az vm list-vm-resize-options \
--name myvm-ent-xx \
--resource-group myresourcegroup
  • To change VM size use the following command: 
az vm resize \
--name myvm-ent-xx \
--resource-group myresourcegroup \
--size Standard_DS2_v2

Replace ‘xx’ by '01', '02', '03’ and so on. 

2) Increase OS-disk size up to 80 GB

  • Obtain OS-disk size details: 
az disk list \
--resource-group myresourcegroup \
--query "[?starts_with(name,'myvm-ent-xx-osdisk')].{Name:name,Gb:diskSizeGb}"
  • Deallocate VM temporarily: 
az vm deallocate \
--name myvm-ent-xx \
--resource-group myresourcegroup
  • Increase OS-disk size: 
az disk update \
--name myvm-ent-xx-osdisk \
--resource-group myresourcegroup \
--size-gb 80
  • Verify new OS-disk size: 
az disk list \
--resource-group myresourcegroup \
--query "[?starts_with(name,'myvm-ent-xx-osdisk')].{Name:name,Gb:diskSizeGb}"
  • Start VM: 
az vm start \
--name myvm-ent-xx \
--resource-group myresourcegroup
  • Connect to VM: 
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com
  • Increase ‘/’ filesystem partition size:
(echo d; echo 2; echo n; echo p; echo 2; echo ; echo ; echo w) | sudo fdisk /dev/sda
  • Restart VM:
sudo reboot
  • Connect to VM: 
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com
  • Increase ‘/’ filesystem size:
sudo xfs_growfs -d /dev/sda2
  • Restart VM:
sudo reboot

Replace ‘xx’ by '01', '02', '03’ and so on. 

3) Use dedicated datadisk for Postgres Pro database files to improve database performance

  • Let's create new 200 GB datadisk and attach it to VM:
az vm disk attach \
--disk myvm-ent-xx-datadisk \
--resource-group myresourcegroup \
--vm-name myvm-ent-xx \
--caching ReadOnly \
--lun 1 \
--new \
--size-gb 200
  • Connect to VM:
ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com
  • Stop Postgres Pro database service and verify its status:
sudo systemctl stop postgrespro-enterprise-9.6.service
sudo systemctl -l status postgrespro-enterprise-9.6.service
or
sudo systemctl stop postgrespro-ent-10.service
sudo systemctl -l status postgrespro-ent-10.service
or
sudo systemctl stop postgrespro-ent-11.service
sudo systemctl -l status postgrespro-ent-11.service
  • Create new filesystem mountpoint:
sudo mkdir /PGDATA
  • Use 'lsscsi' utility to find out datadisk device name (in this case it is '/dev/sdc'):
lsscsi
  • Create datadisk single partition of maximum size, create new filesystem on top of it:
(echo n; echo p; echo 1; echo ; echo ; echo w) | sudo fdisk /dev/sdc
sudo mkfs -t ext4 /dev/sdc1
  • Amend /etc/fstab file for new filesystem automount and mount it:
sudo sh -c "echo '`sudo blkid -o export /dev/sdc1 | grep UUID` /PGDATA ext4 defaults,nofail,barrier=0 1 2' >> /etc/fstab"
sudo mount /PGDATA
  • Create 'data' directory on the new filesystem and set its permissions:
sudo mkdir /PGDATA/data
sudo chown postgres:postgres /PGDATA/data
sudo chmod 0700 /PGDATA/data
  • Switch to ‘postgres’ account and move Postgres Pro database files to the new filesystem:
sudo su - postgres
mv /var/lib/pgproee/9.6/data/* /PGDATA/data; rmdir /var/lib/pgproee/9.6/data; ln -s /PGDATA/data /var/lib/pgproee/9.6/data
or
mv /var/lib/pgpro/ent-10/data/* /PGDATA/data; rmdir /var/lib/pgpro/ent-10/data; ln -s /PGDATA/data /var/lib/pgpro/ent-10/data
or
mv /var/lib/pgpro/ent-11/data/* /PGDATA/data; rmdir /var/lib/pgpro/ent-11/data; ln -s /PGDATA/data /var/lib/pgpro/ent-11/data
exit
  • Start Postgres Pro database service and verify its status:
sudo systemctl start postgrespro-enterprise-9.6.service
sudo systemctl -l status postgrespro-enterprise-9.6.service
or
sudo systemctl start postgrespro-ent-10.service
sudo systemctl -l status postgrespro-ent-10.service
or
sudo systemctl start postgrespro-ent-11.service
sudo systemctl -l status postgrespro-ent-11.service
  • Restart VM, check filesystem automount and verify Postgres Pro database service status:
sudo reboot

ssh azureuser@myvm-ent-xx-dnsname.northeurope.cloudapp.azure.com

sudo mount | grep /PGDATA
sudo df -h /PGDATA

sudo systemctl -l status postgrespro-enterprise-9.6.service
or
sudo systemctl -l status postgrespro-ent-10.service
or
sudo systemctl -l status postgrespro-ent-11.service

Replace ‘xx’ by '01', '02', '03’ and so on.

4) Database service auto restart in case of database failure

  • Edit database service systemd file and restart database service:
sudo sed -i '/KillSignal=/a Restart=on-failure' /usr/lib/systemd/system/postgrespro-enterprise-9.6.service
sudo systemctl daemon-reload
sudo systemctl restart postgrespro-enterprise-9.6.service
sudo systemctl -l status postgrespro-enterprise-9.6.service
or
sudo sed -i '/KillSignal=/a Restart=on-failure' /usr/lib/systemd/system/postgrespro-ent-10.service
sudo systemctl daemon-reload
sudo systemctl restart postgrespro-ent-10.service
sudo systemctl -l status postgrespro-ent-10.service
or
sudo sed -i '/KillSignal=/a Restart=on-failure' /usr/lib/systemd/system/postgrespro-ent-11.service
sudo systemctl daemon-reload
sudo systemctl restart postgrespro-ent-11.service
sudo systemctl -l status postgrespro-ent-11.service

 

VM backup/restore

Let's consider how to do a VM backup/restore in Azure.

You can find more information about it at https://docs.microsoft.com/en-us/azure/backup/quick-backup-vm-cli.

  • To take file-consistent backup of VM, create Recovery Services vault first:
az backup vault create \
--name myvault-ent-xx \
--resource-group myresourcegroup \
--location northeurope

'myvault-ent-xx' Recovery Services vault name will be used further.

  • Change Recovery Services vault storage type to Locally-Redundant if you don't plan to use Geo-Redundant storage:
az backup vault backup-properties set \
--name myvault-ent-xx \
--resource-group myresourcegroup \
--backup-storage-redundancy LocallyRedundant
  • Enable VM backup protection with default backup policy, it will schedule VM backup once a day:
az backup protection enable-for-vm \
--vm myvm-ent-xx \
--vault-name myvault-ent-xx \
--policy-name DefaultPolicy \
--resource-group myresourcegroup
  • Run the first backup manually, specifying recovery point availability date in 'dd-mm-yyyy' format. Provide VM name both for '--item-name' and '--container-name' parameters.
az backup protection backup-now \
--item-name myvm-ent-xx \
--container-name myvm-ent-xx \
--vault-name myvault-ent-xx \
--resource-group myresourcegroup \
--retain-until 31-12-2020
  • Monitor backup job status:
az backup job list \
--vault-name myvault-ent-xx \
--resource-group myresourcegroup
  • After backup job is successfully finished, you can use this backup as a recovery point to restore VM disk. Create Azure storage account if you don't have it:
az storage account create \
--name mystorageaccountent \
--resource-group myresourcegroup \
--location northeurope \
--sku Standard_LRS

'mystorageaccountent' Azure storage account name will be used further.

  • Supply the latest recovery point for restore:
rp_id=$(az backup recoverypoint list --item-name myvm-ent-xx --container-name myvm-ent-xx --vault-name myvault-ent-xx --resource-group myresourcegroup --query [0].name --output tsv)

az backup restore restore-disks \
--item-name myvm-ent-xx \
--container-name myvm-ent-xx \
--vault-name myvault-ent-xx \
--resource-group myresourcegroup \
--storage-account mystorageaccountent \
--rp-name $rp_id
  • Monitor restore job status:
az backup job list \
--vault-name myvault-ent-xx \
--resource-group myresourcegroup
  • Once restore is secured in storage account, supply required information to create VM disk:
container_id=$(az storage container list --account-name mystorageaccountent --query [0].name -o tsv)
blob_id=$(az storage blob list --container-name $container_id --account-name mystorageaccountent --query [0].name -o tsv)
uri_id=$(az storage blob url --name $blob_id --container-name $container_id --account-name mystorageaccountent -o tsv)

az disk create \
--name myrestoredvm-ent-xx-osdisk \
--resource-group myresourcegroup \
--source $uri_id
  • Create VM 'myrestoredvm-ent-xx' from restored disk:
az vm create \
--name myrestoredvm-ent-xx \
--resource-group myresourcegroup \
--attach-os-disk myrestoredvm-ent-xx-osdisk \
--location northeurope \
--size Standard_DS1_v2 \
--public-ip-address-dns-name myrestoredvm-ent-xx-dnsname \
--os-type linux
  • Now you can connect to restored VM:
ssh azureuser@myrestoredvm-ent-xx-dnsname.northeurope.cloudapp.azure.com

Replace ‘xx’ by '01', '02', '03’ and so on.

 

Postgres Pro Enterprise Multimaster

Multimaster is a Postgres Pro Enterprise extension with a set of patches that turns Postgres Pro Enterprise into a synchronous shared-nothing cluster to provide Online Transaction Processing (OLTP) scalability for read transactions and high availability with automatic disaster recovery.

You can find more information about Postgres Pro Enterprise Multimaster at:

https://postgrespro.com/docs/enterprise/9.6/multimaster.html

or

https://postgrespro.com/docs/enterprise/10/multimaster

or

https://postgrespro.com/docs/enterprise/11/multimaster

Let's proceed with 3-node Multimaster installation and configuration. Assume Multimaster node names are 'myvm-ent-01', 'myvm-ent-02' and 'myvm-ent-03'.

  • Create resource group 'myresourcegroup':
az group create \
--name myresourcegroup \
--location northeurope
az vm availability-set create \
--name myavailabilityset \
--resource-group myresourcegroup \
--platform-fault-domain-count 3 \
--platform-update-domain-count 3
  • Create virtual local network 'myvnet' 10.0.0.0/8 and subnetwork 'myvnetsubnet' 10.0.0.0/24:
az network vnet create \
--name myvnet \
--location northeurope \
--resource-group myresourcegroup \
--subnet-name myvnetsubnet \
--subnet-prefix 10.0.0.0/24 \
--address-prefixes 10.0.0.0/8
  • Create network security group 'mynsg':
az network nsg create \
--name mynsg \
--location northeurope \
--resource-group myresourcegroup
  • Create network security group rules to allow incoming traffic to TCP-ports 22 (ssh), 5433 (Postgres) and 80 (http):
array=(AllowInboundSsh 22 1000 AllowInboundPostgresql 5433 1001 AllowInboundHttp 80 1002 AllowInboundHttps 443 1003); for i in `echo 0 3 6 9`; do
az network nsg rule create \
--name "${array[$i]}" \
--resource-group myresourcegroup \
--nsg-name mynsg \
--access Allow \
--direction Inbound \
--protocol Tcp \
--destination-port-range "${array[$i+1]}" \
--priority "${array[$i+2]}"
done
  • Create dynamic public IP-addresses for VMs:
for i in `seq 1 3`; do
az network public-ip create \
--name myvm-ent-0$i-public-ip \
--location northeurope \
--resource-group myresourcegroup \
--dns-name myvm-ent-0$i-dnsname \
--allocation-method Dynamic
done
  • Create network interfaces for VMs and assign dynamic public and static private IP-addresses to them:
for i in `seq 1 3`; do
az network nic create \
--name myvm-ent-0$i-nic \
--location northeurope \
--resource-group myresourcegroup \
--vnet-name myvnet \
--subnet myvnetsubnet \
--network-security-group mynsg \
--public-ip-address myvm-ent-0$i-public-ip \
--private-ip-address 10.0.0.10$i
done

Private IP-addresses assignment: 10.0.0.101 (myvm-ent-01), 10.0.0.102 (myvm-ent-02), 10.0.0.103 (myvm-ent-03).

  • Finally, create VMs:
for i in `seq 1 3`; do
az vm create \
--name myvm-ent-0$i \
--resource-group myresourcegroup \
--availability-set myavailabilityset \
--image $urn_id \
--location northeurope \
--size Standard_DS1_v2 \
--ssh-key-value ~/.ssh/id_rsa.pub \
--admin-username azureuser \
--authentication-type ssh \
--nics myvm-ent-0$i-nic \
--os-disk-name myvm-ent-0$i-osdisk
done
  • Now connect to each of three VMs and run the same set of commands on all of them:
ssh azureuser@myvm-ent-01-dnsname.northeurope.cloudapp.azure.com
ssh azureuser@myvm-ent-02-dnsname.northeurope.cloudapp.azure.com
ssh azureuser@myvm-ent-03-dnsname.northeurope.cloudapp.azure.com
  • Configure firewall for Multimaster arbiter traffic via TCP-port '5555':
--- Postgres Pro Enterprise 9.6/10
sudo firewall-cmd --zone=public --add-port=5555/tcp
sudo firewall-cmd --zone=public --permanent --add-port=5555/tcp
sudo systemctl restart firewalld
  • Configure replicated 'mydb' database:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres
psql -c "create user myuser with superuser encrypted password 'myuserpassword'"
psql --username=myuser -c "create database mydb"
sed -i 's/PGDATABASE=postgres/PGDATABASE=mydb/' .pgpro_profile
sed -i 's/PGUSER=postgres/PGUSER=myuser/' .pgpro_profile
source .pgpro_profile

for i in `seq 1 3`; do
echo "hostssl replication myuser myvm-ent-0$i md5" >> $PGDATA/pg_hba.conf
echo "myvm-ent-0$i:5433:mydb:myuser:myuserpassword" >> ~/.pgpass
done
chmod 0600 ~/.pgpass
pg_ctl reload

--- Postgres Pro Enterprise 9.6/10
node_id=`hostname | awk -F "-" '{ print substr($3,2,1) }'`
echo "" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf
echo "#------------------------------------------------------------------------------" >> $PGDATA/postgresql.conf
echo "# MULTIMASTER SETTINGS" >> $PGDATA/postgresql.conf
echo "#------------------------------------------------------------------------------" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf
echo "multimaster.cluster_name = mymmts" >> $PGDATA/postgresql.conf
echo "multimaster.max_nodes = 3" >> $PGDATA/postgresql.conf
echo "multimaster.node_id = $node_id" >> $PGDATA/postgresql.conf
echo "multimaster.arbiter_port = 5555" >> $PGDATA/postgresql.conf
echo "multimaster.conn_strings = 'dbname=mydb user=myuser host=myvm-ent-01 port=5433 sslmode=require arbiter_port=5555,dbname=mydb user=myuser host=myvm-ent-02 port=5433 sslmode=require arbiter_port=5555,dbname=mydb user=myuser host=myvm-ent-03 port=5433 sslmode=require arbiter_port=5555'" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf

--- Postgres Pro Enterprise 11
echo "" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf
echo "#------------------------------------------------------------------------------" >> $PGDATA/postgresql.conf
echo "# MULTIMASTER SETTINGS" >> $PGDATA/postgresql.conf
echo "#------------------------------------------------------------------------------" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf
echo "multimaster.max_nodes = 3" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf
echo "" >> $PGDATA/postgresql.conf

--- Postgres Pro Enterprise 9.6/10/11
psql -c "alter system set default_transaction_isolation to 'read committed'"
psql -c "alter system set wal_level to logical"
psql -c "alter system set max_connections to 100"
psql -c "alter system set max_prepared_transactions to 300"
psql -c "alter system set max_wal_senders to 10"
psql -c "alter system set max_replication_slots to 10"
psql -c "alter system set max_worker_processes to 250"
psql -c "alter system set shared_preload_libraries to multimaster,pg_stat_statements,pg_buffercache,pg_wait_sampling"

--- Postgres Pro Enterprise 11
psql -c "alter system set wal_sender_timeout to 0"

--- Postgres Pro Enterprise 9.6/10/11
exit
  • Configure mamonsu agent for 'mydb' database and restart mamonsu service:
--- Postgres Pro Enterprise 9.6/10/11
sudo sed -i 's|user = mamonsu|user = myuser|' /etc/mamonsu/agent.conf
sudo sed -i 's|database = mamonsu|database = mydb|' /etc/mamonsu/agent.conf
sudo systemctl restart mamonsu.service
  • Restart Postgres Pro database service and verify its status:
--- Postgres Pro Enterprise 9.6/10/11
sudo systemctl restart postgrespro-enterprise-9.6.service
sudo systemctl -l status postgrespro-enterprise-9.6.service
or
sudo systemctl restart postgrespro-ent-10.service
sudo systemctl -l status postgrespro-ent-10.service
or
sudo systemctl restart postgrespro-ent-11.service
sudo systemctl -l status postgrespro-ent-11.service

exit
  • Now connect to any of three VMs:
--- Postgres Pro Enterprise 9.6/10/11
ssh azureuser@myvm-ent-01-dnsname.northeurope.cloudapp.azure.com
  • and create Multimaster extension:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres
psql
create extension if not exists multimaster;

--- Postgres Pro Enterprise 11
select mtm.init_cluster('dbname=mydb user=myuser host=myvm-ent-01 port=5433 sslmode=require','{"dbname=mydb user=myuser host=myvm-ent-02 port=5433 sslmode=require", "dbname=mydb user=myuser host=myvm-ent-03 port=5433 sslmode=require"}');

--- Postgres Pro Enterprise 9.6/10/11
\q
  • Create other extensions required for mamonsu service:
--- Postgres Pro Enterprise 9.6/10/11
psql -c "create extension if not exists pg_buffercache"
psql -c "create extension if not exists pg_stat_statements"
psql -c "create extension if not exists pg_wait_sampling"
  • Verify the extensions have been successfully created:
--- Postgres Pro Enterprise 9.6/10/11
psql --host=myvm-ent-01 -c "select * from pg_extension"
psql --host=myvm-ent-02 -c "select * from pg_extension"
psql --host=myvm-ent-03 -c "select * from pg_extension"
  • Configure mamonsu for Multimaster:
--- Postgres Pro Enterprise 9.6/10/11
mamonsu bootstrap --dbname mydb --username postgres --host 127.0.0.1 --port 5433 --mamonsu-username=myuser
psql --host=myvm-ent-01 -c "select mtm.make_table_local('mamonsu_config')"
psql --host=myvm-ent-01 -c "select mtm.make_table_local('mamonsu_timestamp_master_2_3_4')"
  • Use the following commands to monitor Multimaster status:
--- Postgres Pro Enterprise 9.6/10
psql --host=myvm-ent-01 -x -c "select mtm.collect_cluster_info()"
psql --host=myvm-ent-01 -x -c "select mtm.get_nodes_state()"

psql --host=myvm-ent-01 -x -c "select mtm.get_cluster_state()"
psql --host=myvm-ent-02 -x -c "select mtm.get_cluster_state()"
psql --host=myvm-ent-03 -x -c "select mtm.get_cluster_state()"

--- Postgres Pro Enterprise 11
psql --host=myvm-ent-01 -x -c "select mtm.status()"
psql --host=myvm-ent-02 -x -c "select mtm.status()"
psql --host=myvm-ent-03 -x -c "select mtm.status()"

psql --host=myvm-ent-01 -x -c "select mtm.nodes()"
psql --host=myvm-ent-02 -x -c "select mtm.nodes()"
psql --host=myvm-ent-03 -x -c "select mtm.nodes()"
  • To return to Azure CLI 2.0 interface run 'exit' command twice

 

External connection to Postgres Pro Enterprise Multimaster database

Let's look at how to connect to Postgres Pro Enterprise Multimaster database via Azure Load Balancer.

You can find more information about it at https://docs.microsoft.com/en-us/azure/load-balancer/load-balancer-overview.

  • Create load balancer public IP-address 'myvm-ent-lb-public-ip' and DNS-name 'myvm-ent-lb-dnsname':
az network public-ip create \
--name myvm-ent-lb-public-ip \
--dns-name myvm-ent-lb-dnsname \
--resource-group myresourcegroup
  • Create load balancer 'myloadbalancer':
az network lb create \
--name myloadbalancer \
--resource-group myresourcegroup \
--frontend-ip-name myFrontEndPool \
--backend-pool-name myBackEndPool \
--public-ip-address myvm-ent-lb-public-ip
  • Create load balancer health probe 'PostgresqlHealthProbe' for Postgres Pro database service:
az network lb probe create \
--name PostgresqlHealthProbe \
--lb-name myloadbalancer \
--resource-group myresourcegroup \
--protocol tcp \
--port 5433
  • Create load balancer rule 'PostgresqlLoadBalancerRule' to distribute incoming requests to Postgres Pro database to Multimaster nodes:
az network lb rule create \
--name PostgresqlLoadBalancerRule \
--lb-name myloadbalancer \
--probe-name PostgresqlHealthProbe \
--resource-group myresourcegroup \
--protocol tcp \
--frontend-port 5433 \
--backend-port 5433 \
--frontend-ip-name myFrontEndPool \
--backend-pool-name myBackEndPool
  • Now add Multimaster nodes to load balancer configuration:
for i in `seq 1 3`; do
ipconfig_id=$(az network nic list --resource-group myresourcegroup --output tsv --query "[?starts_with(name,'myvm-ent-0$i')].[ipConfigurations[0].name]")
az network nic ip-config address-pool add \
--nic-name myvm-ent-0$i-nic \
--ip-config-name $ipconfig_id \
--lb-name myloadbalancer \
--resource-group myresourcegroup \
--address-pool myBackEndPool
done
  • Connect to Postgres Pro Enterprise Multimaster database via load balancer:
psql --host=myvm-ent-lb-dnsname.northeurope.cloudapp.azure.com --port=5433 --user=myuser --dbname=mydb

 

Postgres Pro Enterprise Multimaster (2-node + referee node configuration)

If 3-node Multimaster is an overkill configuration (triple storage size for database), it's still possible to have 2-node Multimaster configuration either by nominating one of two nodes a 'major' node (multimaster.major_node=on) or by using a light-weight node as a referee instead of the third node.

The way to migrate from 3-node Multimaster to 2-node + referee node Multimaster is the following.

  • Exclude 'myvm-ent-03' node from Multimaster configuration and remove Multimster settings from 'myvm-ent-03' referee node:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres
sed -i '/multimaster/d' $PGDATA/postgresql.conf
psql --host=myvm-ent-03 -c "alter system set shared_preload_libraries to pg_stat_statements,pg_buffercache,pg_wait_sampling"

--- Postgres Pro Enterprise 9.6/10
psql --host=myvm-ent-01 -c "select mtm.stop_node(3, true)"
psql --host=myvm-ent-01 -x -c "select mtm.collect_cluster_info()"

--- Postgres Pro Enterprise 11
psql --host=myvm-ent-01 -c "select mtm.drop_node(3)"
psql --host=myvm-ent-01 -x -c "select mtm.nodes()"

--- Postgres Pro Enterprise 9.6/10/11
exit

sudo systemctl restart postgrespro-enterprise-9.6.service
or
sudo systemctl restart postgrespro-ent-10.service
or
sudo systemctl restart postgrespro-ent-11.service

sudo su - postgres
psql --host=myvm-ent-03 -c "drop extension multimaster"
psql --host=myvm-ent-03 -c "create extension referee"
exit

sudo systemctl restart postgrespro-enterprise-9.6.service
or
sudo systemctl restart postgrespro-ent-10.service
or
sudo systemctl restart postgrespro-ent-11.service
  • Change Multimster settings on 'myvm-ent-01' node:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres
psql --host=myvm-ent-01 -c "alter system set multimaster.referee_connstring = 'dbname=mydb user=myuser host=myvm-ent-03 port=5433 sslmode=require'"

--- Postgres Pro Enterprise 9.6/10
psql --host=myvm-ent-01 -c "alter system set multimaster.conn_strings = 'dbname=mydb user=myuser host=myvm-ent-01 port=5433 sslmode=require arbiter_port=5555,dbname=mydb user=myuser host=myvm-ent-02 port=5433 sslmode=require arbiter_port=5555'"

--- Postgres Pro Enterprise 9.6/10/11
exit

sudo systemctl restart postgrespro-enterprise-9.6.service
or
sudo systemctl restart postgrespro-ent-10.service
or
sudo systemctl restart postgrespro-ent-11.service
  • and on 'myvm-ent-02' node:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres
psql --host=myvm-ent-02 -c "alter system set multimaster.referee_connstring = 'dbname=mydb user=myuser host=myvm-ent-03 port=5433 sslmode=require'"

--- Postgres Pro Enterprise 9.6/10
psql --host=myvm-ent-02 -c "alter system set multimaster.conn_strings = 'dbname=mydb user=myuser host=myvm-ent-01 port=5433 sslmode=require arbiter_port=5555,dbname=mydb user=myuser host=myvm-ent-02 port=5433 sslmode=require arbiter_port=5555'"

--- Postgres Pro Enterprise 9.6/10/11
exit

sudo systemctl restart postgrespro-enterprise-9.6.service
or
sudo systemctl restart postgrespro-ent-10.service
or
sudo systemctl restart postgrespro-ent-11.service
  • Finally, drop replication slots and finalize settings on 'myvm-ent-03' referee node:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres

--- Postgres Pro Enterprise 9.6/10
psql --host=myvm-ent-03 -c "select * from pg_replication_slots"
psql --host=myvm-ent-03 -c "select pg_drop_replication_slot('mtm_slot_1')"
psql --host=myvm-ent-03 -c "select pg_drop_replication_slot('mtm_slot_2')"
psql --host=myvm-ent-03 -c "select * from pg_replication_slots"

--- Postgres Pro Enterprise 9.6/10/11
sed -i '/^#/!d' $PGDATA/postgresql.auto.conf
echo "shared_preload_libraries = 'pg_stat_statements, pg_buffercache, pg_wait_sampling'" >> $PGDATA/postgresql.auto.conf
exit

sudo systemctl restart postgrespro-enterprise-9.6.service
or
sudo systemctl restart postgrespro-ent-10.service
or
sudo systemctl restart postgrespro-ent-11.service
  • Check the status of Multimaster on each node:
--- Postgres Pro Enterprise 9.6/10/11
sudo su - postgres

--- Postgres Pro Enterprise 9.6/10
psql --dbname=mydb --username=myuser --host=myvm-ent-01 --port=5433 -c "select mtm.collect_cluster_info()"
psql --dbname=mydb --username=myuser --host=myvm-ent-02 --port=5433 -c "select mtm.collect_cluster_info()"

--- Postgres Pro Enterprise 11
psql --dbname=mydb --username=myuser --host=myvm-ent-01 --port=5433 -x -c "select mtm.nodes()"
psql --dbname=mydb --username=myuser --host=myvm-ent-02 --port=5433 -x -c "select mtm.nodes()"

--- Postgres Pro Enterprise 9.6/10/11
psql --dbname=mydb --username=myuser --host=myvm-ent-03 --port=5433 -c "select * from referee.decision"

exit

 

Postgres Pro Enterprise CFS (compressed file system):

In order to use functionality of Postgres Pro Enterprise CFS (compressed file system) extension, proceed with the following commands.

  • Create OS filesystem directory for 'cfs_ts' tablespace and 'cfs_ts' tablespace in the database: 
sudo su - postgres
mkdir $PGDATA/../cfs_ts
chmod 0700 $PGDATA/../cfs_ts

psql -c "create tablespace cfs_ts location '/var/lib/pgproee/9.6/cfs_ts' with (compression=true)"
or
psql -c "create tablespace cfs_ts location '/var/lib/pgpro/ent-10/cfs_ts' with (compression=true)"
or
psql -c "create tablespace cfs_ts location '/var/lib/pgpro/ent-11/cfs_ts' with (compression=true)"

exit
  • Make sure the 'cfs_ts' tablespace has been created with 'compression=true' option: 
sudo su - postgres
psql -c "select * from pg_tablespace"
exit

Use one of the following ways to utilise 'cfs_ts' tablespace for new database objects:

  • Upon database object creation: 
sudo su - postgres
psql -c "create table t1 (t int) tablespace cfs_ts"
psql -c "select tablename, tablespace from pg_tables where schemaname = 'public'"
exit
  • Setting default tablespace for current database connection: 
sudo su - postgres
psql
set default_tablespace=cfs_ts;
show default_tablespace;
create table t2 (t int);
select tablename, tablespace from pg_tables where schemaname = 'public';
\q
exit
  • Setting default tablespace for particular database: 
sudo su - postgres
psql --dbname=postgres -c "alter database mydb set tablespace cfs_ts"
psql -c "select datname, dattablespace from pg_database"
exit
  • Setting default tablespace for particular user/role: 
sudo su - postgres
psql --username=postgres -c "alter user myuser set default_tablespace to 'cfs_ts'"
psql -c "select usename, useconfig from pg_user"
psql -c "select rolname, rolconfig from pg_roles"
exit

Use one of the following ways to move existing database objects from one tablespace ('pg_default') to another ('cfs_ts'):

  • One by one:
sudo su - postgres
psql -c "create table t3 (t int)"
psql -c "alter table t3 set tablespace cfs_ts"
psql -c "select tablename, tablespace from pg_tables where schemaname = 'public'"
exit
  • All together:
sudo su - postgres
psql -c "alter table all in tablespace pg_default set tablespace cfs_ts"
psql -c "select tablename, tablespace from pg_tables where schemaname = 'public'"
exit

Depending on the data stored in tablespace 'cfs_ts', commpression ratio may vary.

 

Postgres Pro Enterprise 9.6/10/11 VM content:

Linux CentOS 7.x (64-bit)

  • OS-account - ‘postgres’
  • OS-account - ‘zabbix’
  • OS-account - ‘mamonsu’

OS-disk size - 50 GB

  • xfs filesystem ‘/boot’ (/dev/sda1) - 1 GB
  • xfs filesystem ‘/’ (/dev/sda2) - 49 GB

Main database - Postgres Pro Enterprise

  • DB version: 9.6/10/11
  • TCP-port: 5433 (opened in OS-firewall settings)
  • configuration file: /var/lib/pgsql/.pgpro_profile
  • database account: ’postgres’

Database monitoring (server)

  • zabbix-server version: 3.4
  • TCP-ports: 80/443 (opened in OS-firewall settings)
  • account: ‘Admin’

Database monitoring (agent)

  • zabbix-agent version: 3.4
  • mamonsu-agent version: 2.3.4
  • configuration file: /etc/mamonsu/agent.conf

Auxiliary database PostgreSQL (as a zabbix-server database)

  • DB version: 9.6/10/11
  • TCP-port: 5432
  • configuration file: /var/lib/pgsql/.pgsql_profile
  • database account: 'postgres'

 

Documentation links