How To Install PostGIS on Ubuntu 22.04|20.04|18.04

Posted on 372 views

Today’s guide will cover how to install PostGIS on Ubuntu 22.04|20.04|18.04 Linux. PostGIS is an open source PostgreSQL database extension for creating Geographic objects on the PostgreSQL object relational database. The features of PostGIS are derived from Open Geospatial Consortium SQL specifications.

PostGIS can be installed on Ubuntu, Debian, SUSE Linux, Red Hat based Linux systems such as CentOS, Scientific Linux, Windows and macOS. The PostgreSQL build team has packages for Debian and Ubuntu for several versions of PostgreSQL. We’ll add the PostGIS APT repository for PostgreSQL and install PostGIS on Ubuntu 22.04|20.04|18.04 from it.

Let’s now kickstart the installation of PostGIS on Ubuntu 22.04|20.04|18.04 Linux system.

Step 1: Update system

You can do APT package list update and system upgrade before you get started.

sudo apt update
sudo apt -y upgrade

A reboot is necessary after an upgrade.

sudo reboot

Step 2: Add PostgreSQL repository

Before you start the installation of PostGIS on Ubuntu 20.04|18.04 Linux, you’ve to add PostgreSQL APT repository to your system where binary packages will be downloaded and installed.

sudo apt -y install gnupg2
wget --quiet -O - | sudo apt-key add -

After importing GPG key, add PostgreSQL APT repository.

echo "deb `lsb_release -cs`-pgdg main" |sudo tee  /etc/apt/sources.list.d/pgdg.list

Step 3: Install PostgreSQL Database Server

PostgreSQL is required to be installed before installation of PostGIS on Debian / Ubuntu. Use our previous guides for this installation.

For different version of PostgreSQL, replace version number with your desired installation version.

Step 4: Install PostGIS on Ubuntu22.04|20.04|18.04

After the installation of PostgreSQL, proceed to install PostGIS on Ubuntu22.04|20.04|18.04 Linux distribution.

With PostgreSQL 13:

sudo apt update
sudo apt install postgis postgresql-13-postgis-3

The next is for PostgreSQL 12. Replace 13 with your PostgreSQL version.

sudo apt update
sudo apt install postgis postgresql-12-postgis-3

Step 5: Enable PostGIS on Ubuntu22.04|20.04|18.04

You need to activate PostGIS features on a database before you can store spacial data. The example below will show you how to create a database and activate the Spacial features.

1.Switch to postgres user.

sudo -i -u postgres

2.Create test user/database.

-bash-4.2$ createuser postgis_test
-bash-4.2$ createdb postgis_db -O postgis_test

3.Connect to the test database:

-bash-4.2$ psql -d postgis_db
psql (11.5 (Debian 11.5-3.pgdg100+1))
Type "help" for help.

4.Enable the PostGIS extension on the database:

postgis_db=# CREATE EXTENSION postgis;

5. Verify PostGIS is working

postgis_db=# SELECT PostGIS_version();
(1 row)

For more PostGIS mastery, visit the PostGIS Documentation website.


Gravatar Image
A systems engineer with excellent skills in systems administration, cloud computing, systems deployment, virtualization, containers, and a certified ethical hacker.