Upgrading Postgres on Ubuntu

Since I had a post on how to update Postgres on OS X, I thought I might as well create one for Ubuntu for my own reference, as well.

First of all, take a look at this page to see how you can use the official Postgres Apt packages. It’s important to note that if you use these packages (maybe the ones from Canonical, too, but I’ve never used those), the data directory for Postgres will be put in different locations, and the old version will continue to start if you use the /etc/init.d script. Each version will start on a different port, beginning at 5432 (the default) for the oldest version.

Be sure to change the version numbers below:

sudo apt-get install -y postgresql-$new postgresql-server-dev-$new postgresql-contrib-$new postgresql-client-$new postgresql-doc-$new
sudo -u postgres psql -p 5432 -c "select version()" #make sure your old version is still on port 5432
sudo -u postgres psql -p 5433 -c "select version()" #make sure the new version is on port 5433
sudo -u postgres pg_dumpall -p 5432 > ~/pg_backup.sql #default home path is /var/lib/postgresql
sudo -u postgres psql -p 5433 < ~/pg_backup.sql
sudo -u postgres psql -p 5433 -l #make sure your databases were created successfully
sudo apt-get remove -y postgresql-$old postgresql-server-dev-$old postgresql-contrib-$old postgresql-client-$old postgresql-doc-$old
sudo /etc/init.d/postgresql restart
sudo -u postgres psql -c "select version()" #make sure the new version is on port 5432

Leave a Reply

Your email address will not be published. Required fields are marked *