close up of globe

How to Install PostGIS on Ubuntu 22.04

This short article will learn how to install PostGIS on Ubuntu 22.04. Ubuntu 22.04 is the latest LTS version of Ubuntu. I have written a similar article to install and configure PostGIS on Ubuntu 20.04. You can check that post if you want. Basically, it is a pretty identical step but for the sake of this article, I will update it. If you don’t know, PostGIS is an extension for PostgreSQL that enable us to store spatial data in the PostgreSQL database.

Steps to Install PostGIS on Ubuntu 22.04

Step 1. Update Ubuntu

It is a standard task that we need to update our system prior to the installation.

sudo apt update

Step 2. Install PostgreSQL

Now let’s install PostgreSQL and all its dependency files.

sudo apt install postgresql postgresql-contrib

Let’s start the service

sudo systemctl start postgresql

Step 3. Create a new PostgreSQL role and database

After completing the PostgreSQL installation, we can now create a new user and new database. The following step will create a new user called “gis_database” and a new database called “gis_database

$ sudo -i -u postgres
postges@dhani-ubuntu:~$ createuser --interactive

Now let’s create a new database

postges@dhani-ubuntu:~$ createdb gis_database

You can also do this user and database creation from the psql console.

$ sudo -i -u postgres
postges@dhani-ubuntu:~$ psql
postgres=# CREATE USER gis_database with password '12345';
postgres=# CREATE DATABASE gis_database;
postgres=# GRANT ALL PRIVILEGES ON DATABASE gis_database TO gis_database;

At this point, we have created a new database and role for our PostgreSQL.

Step 4. Install PostGIS

To install PostGIS, run the following command on Terminal

sudo apt install postgis

Once the installation is completed, we need to enable the PostGIS extension on the database. We should enable the PostGIS extension on each database we want. Now we are going to enable the extension to the gis_database we created earlier.

sudo -i -u postgres psql
postgres=#\CONNECT gis_database;
You are now connected to database "gis_database" as user "postgres".
postgres=#CREATE EXTENSION postgis;
CREATE EXTENSION

Done. At this point, we have successfully enabled the postgis extension on the gis_database. Now we can start using this database to store our spatial data.

Step 5. Enable remote access

Last but not least, we need to enable remote access to our PostgreSQL server. By enabling this feature, our network users will be able to access the database and store their data through the network.

sudo nano /etc/postgresql/14/main/pg_hba.conf

At the end of the file, copy and paste this line

host    all             all             0.0.0.0/0              md5

Close and save the file. And then restart Postgresql service

sudo systemctl restart postgresql

Next, edit the postgresql.conf

sudo nano /etc/postgresql/14/main/postgresql.conf

Find the following line:

#listen_addresses = 'localhost'

Change it to:

listen_addresses = '*'

Close and save the file. Finally, restart the service once again.

sudo systemctl restart postgresql

Done.

Step 5. Connect to PostGIS

Now we will connect QGIS to the new PostGIS server we’ve just created. Open QGIS and then right-click PostgreSQL from the Browser. Click New Connection and enter the PostgreSQL server details.

Thanks for reading the article how to install PostGIS on Ubuntu 22.04. I hope this article is useful for you.