Friday, January 8, 2016

Installing PostgreSQL 9.5 on Ubuntu/Kubuntu 15.10 (Wily)

The Ubuntu packages for PostgreSQL 9.5 have been released, but the installation instructions on postgresql.org are a little unclear.  After googling to a very helpful blog post (thanks Raony Cardenas!) and some experimentation, here is the succinct version:

    > sudo vim /etc/apt/sources.list.d/pgdg.list

add a line for the repositorydeb 

    > http://apt.postgresql.org/pub/repos/apt/ wily-pgdg main 

The mystery bit is the name "wily-pgdg"  The first part (wily) should match your Ubuntu release.  You can find out the correct name by using:

    > lsb_release --all
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description: Ubuntu 15.10
    Release: 15.10
    Codename: wily

Import the repository signing key, and update the package lists (cut & paste the following 3 lines)

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

sudo apt-get update

sudo apt-get install -y postgresql-9.5

The last line (with "install") is the one that was missing; everything before that just provides the settings to access the postgresql.org Ubuntu package libraries.

At this point, you should be able to see both your old version of postgresql and the new 9.5 version:

    > alias d="ls -ldF"
    > d /etc/postgresql/*
    drwxr-xr-x 3 postgres postgres 4096 May 26  2015 /etc/postgresql/9.4/
    drwxr-xr-x 3 postgres postgres 4096 Jan  8 08:49 /etc/postgresql/9.5/

If you still have PostgreSQL 9.4 installed (as above), the new 9.5 will not be on the default port of 5432, but will instead take the next one (5433).  You may wish to edit your postgresql.conf to both rearrange the ports (I put the old postgresql on 5439 and the new one on 5432) and also to copy over any modifications to the parameters you have made:

    > sudo vim -d /etc/postgresql/*/main/postgresql.conf

You will also want to copy over any changes to pg_hba.conf:

    > sudo vim -d /etc/postgresql/*/main/pg_hba.conf    

At this point, all you need to do is restart both servers, and away you go!

    > sudo systemctl restart postgresql
    > psql --version
    psql (PostgreSQL) 9.5.0

Try to log in:

      > psql
   psql: FATAL:  role "alan" does not exist

Doh!  We forgot to create our users for the new install:

    > sudo -u postgres createuser alan 
    > sudo -u postgres createdb   alan

Viola!  Now it works:

    > psql
    psql (9.5.0)
    Type "help" for help.
    alan=> select version();
                                                 version                                                  
----------------------------------------------------------------------------------------------------------
 PostgreSQL 9.5.0 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010, 64-bit
(1 row)

alan=> \q

Enjoy!!!