DEV Community

Manish Pradhan for YoungInnovations

Posted on

Maintaining Multiple Postgres Version

Sometimes we need to have different Postgres versions in our local machine. We just need to know the port the versions are running and we are good to go.

For this let's install two versions of Postgres 9.6 and 11 via commands

Install Postgres 9.6

sudo apt-get install postgresql-9.6 
Enter fullscreen mode Exit fullscreen mode

Install Postgres 11

sudo apt-get install postgresql-11
Enter fullscreen mode Exit fullscreen mode

Lists the postgres clusters

pg_lsclusters
Enter fullscreen mode Exit fullscreen mode

start-stop the clusters

sudo pg_ctlcluster 9.6 main start
sudo pg_ctlcluster 9.6 main stop
sudo pg_ctlcluster 11 main start
sudo pg_ctlcluster 11 main stop
Enter fullscreen mode Exit fullscreen mode

connects to 9.6

sudo -i -u postgres psql -p 5432
Enter fullscreen mode Exit fullscreen mode

connects to 11

sudo -i -u postgres psql -p 5433
Enter fullscreen mode Exit fullscreen mode

By changing the port only we can restore and dump db according to postgres version

dumps and restores 9.6

pg_dump --host localhost --port 5432 --username "postgres" --encoding UTF8 --verbose --file "{{db_backup_path}}" "{{db_name}}"
pg_restore --host 127.0.0.1 --port 5432 --username "postgres" --dbname "{{db_name}}" --verbose "{{db.backup}}"
Enter fullscreen mode Exit fullscreen mode

dumps and restores 11

pg_dump --host localhost --port 5433 --username "postgres" --encoding UTF8 --verbose --file "{{db_backup_path}}" "{{db_name}}"
pg_restore --host 127.0.0.1 --port 5433 --username "postgres" --dbname "{{db_name}}" --verbose "{{db.backup}}"
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
nirazanbasnet profile image
⚡ Nirazan Basnet ⚡

Great !!