How do I install PostgreSQL 9.6 on any Ubuntu version?

How do I install PostgreSQL 9.6 on any Ubuntu version since it doesn't come by default with the most recent version?

Ubuntu Xenial (16.04) comes with PostgreSQL 9.5 from the default repositories.


Solution 1:

For the following Ubuntu versions, you can install with the given commands, as per the official PostgreSQL Apt Repository.

Ubuntu 17.04 - 17.10

Version 9.6 comes with the distribution.

sudo apt-get install postgresql-9.6

Ubuntu 14.04, 16.04

sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-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

Solution 2:

Follow below steps:

Reference is taken from this blog.

You need to add the latest PostgreSQL repository for the latest version.

sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main"

Update and Install PostgreSQL 9.6:

sudo apt-get update
sudo apt-get install postgresql-9.6

Default postgres super user and postgres database is created. You need to set a password for the postgres super user.

ubuntu@:~$ sudo passwd postgres
Enter new UNIX password:****
Retype new UNIX password:****
passwd: password updated successfully

If service is not started, you can start the PostgreSQL service.

sudo service postgresql start

Connect PostgreSQL server using postgres user:

ubuntu@:~$ su postgres
Password:****

Create a sample database:

createdb database_name

Connect to that database:

psql -d database_name

Solution 3:

The accepted answer wouldn't work for me on WSL Ubuntu 18.04.

What did work for me was based on https://medium.com/@philip.mutua/how-to-install-postgresql-9-6-on-ubuntu-16-04-and-14-04-lts-5d463da49ea5

essentially:

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 -
sudo apt-get update
sudo apt-get install postgresql-9.6 postgresql-contrib-9.6

I simply added -9.6 to the installed packages.