Generic Postgres Upgrade Document

  • In this page, we have laid out the steps to upgrade postgres using pg_dump and pg_restore

  • This is a generic document, which you can use to upgrade from any version of postgres to any other version

  • We also talk a bit about Azure postgres and some ways to ensure your services dont update the database during upgrade

  • This upgrade procedures requires a downtime

Sl No

Task

Command

Comments

0

Create a new postgres instance

Choose postgres version 10 (preferably 11 ) and above and same configuration as per existing instance on Azure

You are free to have independent instances or a single instance for all services as per your need

Whitelist all the IP's as per existing postgres

Test the connection by connecting to the instance from jenkins

If on VM, create a new VM (preferably Ubuntu 18 and above) and run the Postgres provision role.

Note: This document does not cover details on how to upgrade postgres on the same / exisiting VM. Feel free to contribute the steps to this document.

New instance of same configuration should be created. We have used Postgres version 11

1

Update the inventory

Search and update all the variables in common.yml, secrets.yml Core and DataPipeline directory in private repository

Replace the postgres instance address, postgres user and postgres password with the new details

Also look at variable druid_postgres_user in case you are using Azure postgres. If you are using a common username and password per instance, then this variable needs to be added and the value should be your instance username. This variable will default to druid@instance

If you are on a VM, use the new VM IP along with username and password

It will be simpler if you use same username and password for all dbs on the postgres instance.

If you want to use different user name and password for each database on an instance, then you need to ensure you create those users and roles before hand by running provision jobs against your postgres instance or do them manually.

This document does not cover how to restore user accounts. For that you can refer to Azure docs or postgres documentation. Below is one such link -

https://docs.microsoft.com/en-us/azure/postgresql/how-to-upgrade-using-dump-and-restore

2

Start of down time

Stop traffic / services

Stop traffic

Put Jenkins into maintenance mode so nobody can deploy any jobs

Cut off connection to postgres from all services except Jenkins (In azure remove all connecting subnets except Jenkins, disable Azure services connections also)

In VM you have multiple ways - Disable outside connections by editing pg_hba.conf so only localhost connections are accepted. Or stop the below services so that they dont update postgres database

List of services that are going to be affected on Sunbird -

- Deploy/DataPipeline/AnalyticsCore - Deploy/DataPipeline/CoreDataProducts - Deploy/DataPipeline/EdDataProducts - Deploy/DataPipeline/FlinkPipelineJobs - Deploy/Datapipeline/InternalKong - Deploy/Kubernetes/Analytics - Deploy/Kubernetes/APIManager - Deploy/Kubernetes/Enc - Deploy/Kubernetes/HawkeyeSuperset - Deploy/Kubernetes/Keycloak - Deploy/Kubernetes/Learner - Deploy/Kubernetes/LMS - Deploy/Kubernetes/Report - Deploy/UCI/fusionauth - Deploy/UCI/gql - Deploy/UCI/Inbound - Deploy/UCI/odk - Deploy/UCI/Orchestrator - Deploy/UCI/Outbound - Deploy/UCI/Transformer - Deploy/UCI/UCI - DataPipeline/Druid - Superset

List of services that are going to be affected on Vidyadaan -

- Deploy/Kubernetes/APIManager - Deploy/Kubernetes/Enc - Deploy/Kubernetes/Opensaber - Deploy/Kubernetes/Program - Deploy/Kubernetes/HawkeyeSuperset - Deploy/Kubernetes/PostgresqlMigration - Deploy/DataPipeline/EdDataProducts

Respective services should be stopped / Postgres reachability should be removed

Traffic can be stopped based on situation by removing nginx daemonset or changing nginx service port mapping to something else from 80 / 443

3

Manually list all the DB's first

export PG_HOST="" # Enter the postgres host inside the quotes export PG_USER="" # Enter the postgres user inside the quotes export PGPASSWORD="" # Enter the postgres password inside the quotes psql -h $PG_HOST -U $PG_USER -d postgres -c "\l"

All DB should be listed

4

Run command to get all the DB's on terminal and store in a file

Verify all db are present in the file and matches our db's
Ignore db's like postgres, template0, template1, azure_maintanance and also remove not required DB's from the file

5

Take backup of the DB's from current instance

Replace PG_CPU_CORES with the number of cores of the postgres insatnce.
Example: For 4 core, value will be 4

6

Get count of all tables from all DB's from current instance

All tables and counts will be displayed and also written into the files

Repeat Steps 3 - 6 for every postgres instance (in case you are running the workload on separate instances)

7

Rearrange the DBs across two instances or based on your requirement

In this case, we will be creating the following databases across two instances

Instance 1 - Keycloak, Public Kong, Private Kong, Quartz, Enc Keys
Instance 2 - Analytics, Druid, Graphite

Feel free to add any other dbs you have / want to. This is not an exhaustive list

Create two directories as below

Add the required db names in the file dbs.txt. See next column for sample reference

Move all the required backup folders and files to this directory. Below is a sample command

Similarly do for the other dbs and pg11-db folder also (second instance)

Add the required db names in the file dbs.txt. See next column for sample reference

If you are going to use only once instance, then you can move all the backup data and other files under a single directory

Instance 1:

 

Instance 2:

8

Install postgres 11 tools

If you to use pg_dump and pg_restore on older postgres version, install those packages

sudo apt install postgresql-client-9.6

9

Create empty databases in new instance

Empty databases should be created on new postgres instance

Note: Add double quotes in dbs.txt if you have hyphen in DB name "DB-NAME" or else you will receive below error

10

Restore the DB's to the new instance

Replace PG_CPU_CORES with the number of cores of the postgres insatnce.
Example: For 4 core, value will be 4

Note: Remove double quotes in dbs.txt if you have hyphen in DB name "DB-NAME" or else you will receive below error

Ignore errors like

11

Analyze the db's on new instance

Verbose logs will be displayed

12

Get count of all tables from all DB's from new instance

All tables and counts will be displayed and also written into the files

13

Compare the row counts of both the instances

This will not display any output. Which means files are identical

If there are differences use the below to compare and then take fresh backup and restore those dbs again
diff -y file1 file2

14

Remove all connections to old Postgres

Remove all subnets and enable deny public network acces on Azure portal

If on VM, stop the VM / postgres service

In case of Azure, make sure you update the Backup and Log retention days to a large number

To ensure no service can connect to old DBs

15

Clear offline session from Keycloak DB on Sunbird

If we have too many rows in these tables, keycloak will not start

16

End of down time Redeploy services / Enable Traffic

Redeploy the following services in case of Sunbird:

Redeploy the following services in case of Vidyadaan:

The configmaps and configuration files will be updated with new data. Do verify it by checking them manually for all these services.

Note: If during deployment, any ansible errors occur due to missing variables or incorrect variables, you will have to fix them and move forward

Â