pg_dumpall > backup.out
For Ubuntu 14.04
sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main"
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.6
sudo service postgresql stop
sudo -k -u postgres /usr/lib/postgresql/9.6/bin/pg_upgrade \
-b /usr/lib/postgresql/9.4/bin \
-B /usr/lib/postgresql/9.6/bin \
-d /var/lib/postgresql/9.4/main \
-D /var/lib/postgresql/9.6/main \
-o ' -c config_file=/etc/postgresql/9.4/main/postgresql.conf' \
-O ' -c config_file=/etc/postgresql/9.6/main/postgresql.conf'
Before you run the below command change the ports of both 9.4 and 9.6 if required.
sudo service postgresql start
sudo pg_ctlcluster 9.4 main stop
The below will permanently remove the 9.4 instance. So make sure that everything working as desired before doing this.
sudo pg_dropcluster 9.4 main
Change the configuration of the Postgresql 9.6 in postgresql.conf and pg_hba.conf files and reload.