Install PostgreSQL on Ubuntu 20.04

Did you know that PostgreSQL is available in all Ubuntu versions by default?

Yes, that means that your Ubuntu 20.04 installation probably already has a PostgreSQL package sitting there waiting for you to install it.

And you can install it with one command, which I’ll show you below.

But what if you prefer to run the latest version of Postgres instead? Well, I’ll also show you how to install that. Installing the latest version is almost as easy as installing your local package.

Check your Current PostgreSQL Version

First up, you should probably check the version of the current PostgreSQL package on your Ubuntu 20.04 machine.

Fire up a Terminal and run the following command:

apt show postgresql

Here’s the result I got when I ran that command:

Package: postgresql
Version: 12+214ubuntu0.1
Priority: optional
Section: database
Source: postgresql-common (214ubuntu0.1)
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian PostgreSQL Maintainers
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 67.6 kB
Depends: postgresql-12
Suggests: postgresql-doc
Task: postgresql-server
Download-Size: 3,924 B
APT-Sources: http://au.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages
Description: object-relational SQL database (supported version)
This metapackage always depends on the currently supported PostgreSQL
database server version.
.
PostgreSQL is a fully featured object-relational database management
system. It supports a large part of the SQL standard and is designed
to be extensible by users in many aspects. Some of the features are:
ACID transactions, foreign keys, views, sequences, subqueries,
triggers, user-defined types and functions, outer joins, multiversion
concurrency control. Graphical user interfaces and bindings for many
programming languages are available as well.
N: There is 1 additional record. Please use the '-a' switch to see it

In my case, the PostgreSQL 12 package already exists on my freshly installed Ubuntu 20.04 machine. I have the choice of using that, or installing the latest package from the PostgreSQL Apt Repository.

At the time of running this example, PostgreSQL 13.1 is available.

So let’s go ahead and install the latest version.

Install the Latest Version of PostgreSQL

Installing the latest version of PostgreSQL is done via the PostgreSQL Apt Repository. The following steps is all it takes to install the latest version of PostgreSQL from the repository.

If you prefer to install the local package, jump to How to Install the Local PostgreSQL Version.

Here are the steps I used to install PostgreSQL on Ubuntu 20.04.

Create the file repository configuration:

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

Import the repository signing key:

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

Update the package lists:

sudo apt-get update

Install PostgreSQL:

sudo apt-get -y install postgresql

The latest version of PostgreSQL is now installed on Ubuntu 20.04.

You can see the details of the PostgreSQL installation in the output. Things such as version, port, the data and log directories, etc

The above command installs the latest version of PostgreSQL. If you want to install a specific version, append postgresql with a hyphen, followed by the version number, like this: postgresql-13.

Note that the PostgreSQL Apt Repository integrates with your normal systems and patch management, and provides automatic updates for all supported versions of PostgreSQL throughout the support lifetime of PostgreSQL.

How to Install the Local PostgreSQL Version

If you prefer to install your local version of PostgreSQL (instead of the latest version like above), you can do so with the following command:

sudo apt install postgresql postgresql-contrib

That’s all there is to installing your local version of PostgreSQL on your Ubuntu 20.04 machine.

Connect to PostgreSQL

You can connect to PostgreSQL by running the following command:

sudo -u postgres psql

That command uses the postgres user to connect via the psql command-line utility.

After running that command, you should see something like this:

psql (13.1 (Ubuntu 13.1-1.pgdg20.04+1))
Type "help" for help.
postgres=#

That means you’ve successfully connected to PostgreSQL with the psql utility.

You can now create databases, run queries, run psql commands, etc.

For example, you could run the following psql backslash command to get information about your current connection:

\conninfo

Running that command should return something like this:

You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".

By default, PostgreSQL creates a postgres user and a corresponding postgres database.

When you connect using the postgres user, by default, you’re connected to the postgres database.

You can also connect to Postgres using GUI tools such as PgAdmin, DBeaver, Azure Data Studio (using its PostgreSQL extension).

To install Azure Data Studio, see How to Install Azure Data Studio on Ubuntu 20.04.