Installing Postgresql on Ubuntu

Installing Postgresql on Ubuntu

...the straightforward guide

Whether you're running Ubuntu 14+ or a later version, getting Postgresql installed is a pretty straightforward task; that is, if you have these two key ingredients:

  1. root user privileges or sudo access
  2. the right commands to get the job done

Considering the first, you may be required to speak with an administrator, if you aren't one. But with regards to the second, that's why I'm here. So, let's get on with it.


wget -q -O - | sudo apt-key add - && \
echo "deb $(lsb_release -cs)-pgdg main" | \
sudo tee /etc/apt/sources.list.d/postgres-pgdg.list > /dev/null && \
sudo apt update && sudo apt install postgresql-14

System Update

Firstly, run a quick system update check on your Ubuntu server.

sudo apt update

APT (advanced package tool) handles the installation and removal of software on Debian-based Linux distributions, like Ubuntu.

Add Repository Signing Key

Next, add the GPG key signature that verifies the signature of any downloaded Postgresql file release.

wget -q -O - | sudo apt-key add -

wget will quietly (-q) download and output the key to stdout (-O -), after which, it will pipe it to apt-key for storage.

Create File Configuration

Since the latest version of postgresql lives on a repository elsewhere, it is required that you add that location to your sources.list.d directory.

echo "deb $(lsb_release -cs)-pgdg main" | sudo tee /etc/apt/sources.list.d/postgres-pgdg.list

echo outputs the string in quotes (") to stdout, slotting in the lsb_release which returns the LSB (Linux Standard Base) codename (-c), while omitting the headers (-s); and then pipes the output to tee which sends the result to the postgres-pgdg.list file. As for $(), it is a command substitution technique, which executes its input in-place.

Refresh Package Index

Running an update fetches the available package index files from the newly added location in the sources.d.list directory.

sudo apt update

Installing Postgresql

The postgresql repository contains many different packages including third party addons. However, depending on the version of postgresql you intend to install, you only need to append the version number, taking note of the latest available version, which you can check here.

sudo apt install postgresql-14

And there you have it, a dead-simple guide on installing the latest version of Postgresql. For an insight into accessing Postgresql via the terminal, make sure to also read my next article, "Postgresql, Up and Running".

Till next time, happy hacking.