Install the latest PostgreSQL version on Ubuntu 18.04

The Global Development Group of PostgreSQL is maintaining their own apt repo. Instead of using the default and outdated PostgreSQL packages that ship with Ubuntu 18.04 we can always get the latest version by adding this repository. This way we are also flexible and can specify which version of PostgreSQL we want to install.

Import the PostgreSQL repository key

To make sure and validate we receive the PostgreSQL packages from the real owner we will add the following apt key to our keyring. This way we make sure we don't get injected bad packages.

sudo apt install curl ca-certificates gnupg
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

Add the repo to the sources list

Depending on your distribution and version we need to add a different codename to the sources list. Thanks to "lsb_release -c" we don't need to manually specifiy it, we can simply get the distribution with that command.

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

Install the latest PostgreSQL version

To reflect the changes we just did to the sources list we need to simply update the packages and afterwards install the desired PostgreSQL version.

apt update
apt install postgresql-11

Source: wiki.postgresql.org