We can always install postgresql with fresh database, but the problem arise when we need to upgrade psql retaining all the databases and the data within it. For that to accomplish, following are the steps you need to follow :
- Stop the postgresql already running :
$ sudo /etc/init.d/postgresql stop
- Setup the correct repository; add the keys for correct version to be installed.
$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
$ wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
- Update the system to implement addition of keys and install new packages
$ sudo apt-get update
$ sudo apt-get install postgresql postgresql-contrib
Now when you run this command :
you will see 2 clusters, 1 for the old version already installed and other is the new version we installed now. Fow me the old one was 9.3 and new is 9.6
- Now we will be droping our new version and upgrade the old version as we need to retain our old database cluster.
$ sudo pg_dropcluster 9.6 main --stop
$ sudo pg_upgradecluster 9.3 main
- Now since our old cluster is upgraded we can drop it.
$ sudo pg_dropcluster 9.3 main
Confirm the cluster working with this :
There should be only one cluster, the latest one, one we want to work with.
Also check if all your databases exist in upgraded postgres version. They should. 😉