This tutorial will guide you to install PostGIS on Ubuntu 24.04. PostGIS is a PostgreSQL extension that will enable us to store spatial data into the PostgreSQL database. It is a great solution for GIS users because we can directly open, import, export, and even edit the spatial data in PostgreSQL using QGIS, ArcGIS Pro and so on. So in this tutorial, we will show you step by step to get PostGIS installed on Ubuntu 24.04.
PostGIS is a spatial database extender for PostgreSQL, enabling it to store, query, and analyze geographic and geometric data. It’s a powerful tool for building location-based applications, handling everything from simple points to complex geospatial queries.
We can install PostgreSQL on most Linux and Windows system. But here, we will focus on Ubuntu 24.04. So I assume you have this distribution installed in your system. Next, lets continue to the installation process.
Steps to Install PostGIS on Ubuntu 24.04
Step 1. Update Ubuntu
Open Terminal and type this command to update Ubuntu
sudo apt update && sudo apt upgrade
Step 2. Install PostgreSQL
Use this command to install PostgreSQL in Ubuntu 24.04
sudo apt install postgresql postgresql-contrib
Output:
dhani@ubuntu-postgis:~$ sudo apt install postgresql postgresql-contrib
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
libcommon-sense-perl libjson-perl libjson-xs-perl libllvm17t64 libpq5 libtypes-serialiser-perl postgresql-16 postgresql-client-16 postgresql-client-common postgresql-common ssl-cert
Suggested packages:
postgresql-doc postgresql-doc-16
The following NEW packages will be installed:
libcommon-sense-perl libjson-perl libjson-xs-perl libllvm17t64 libpq5 libtypes-serialiser-perl postgresql postgresql-16 postgresql-client-16 postgresql-client-common postgresql-common postgresql-contrib
ssl-cert
0 upgraded, 13 newly installed, 0 to remove and 2 not upgraded.
Need to get 43.5 MB of archives.
After this operation, 175 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Type Y and press Enter to confirm. The installation will start.
Start PostgreSQL Service
sudo systemctl start postgresql
Step 3. Create a New Role and Database
To create a new role and new database in PostgreSQL, we will do this using psql.
sudo -i -u postgres
psql
Now lets create a new user/role
postgres=# CREATE USER gis;
The command will create a new user gis.
postgres=# CREATE DATABASE gis;
This will create a new database called gis. Now let’s check the database we’ve created.
postgres=# \l
Output
Now let’s grant all the privileges for gis user to the gis database.
postgres=# GRANT ALL PRIVILEGES ON DATABASE gis TO gis;
Step 4. Install and Enable PostGIS Extension
PostGIS extension must be enabled inside the database. So we are going to enable the extension in the gis database we created in the previous step. But first, we need to install the extension in Ubuntu 24.04.
sudo apt install postgis
Output:
dhani@ubuntu-postgis:~$ sudo apt install postgis
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
gdal-data gdal-plugins libaec0 libarmadillo12 libarpack2t64 libblas3 libblosc1 libboost-serialization1.83.0 libcfitsio10t64 libfreexl1
libfyba0t64 libgdal34t64 libgeos-c1t64 libgeos3.12.1t64 libgeotiff5 libgfortran5 libgif7 libgmpxx4ldbl libhdf4-0-alt libhdf5-103-1t64
libhdf5-hl-100t64 libkmlbase1t64 libkmldom1t64 libkmlengine1t64 liblapack3 liblcms2-2 libltdl7 libminizip1t64 libmysqlclient21
libnetcdf19t64 libodbc2 libodbcinst2 libogdi4.1 libopenjp2-7 libpoppler134 libproj25 libqhull-r8.0 librttopo1 libsfcgal1t64 libsnappy1v5
libspatialite8t64 libsuperlu6 libsz2 liburiparser1 libxerces-c3.2t64 mysql-common poppler-data postgis-doc postgresql-16-postgis-3
postgresql-16-postgis-3-scripts postgresql-postgis postgresql-postgis-scripts proj-bin proj-data unixodbc-common
Suggested packages:
geotiff-bin gdal-bin libgeotiff-epsg libhdf4-doc libhdf4-alt-dev hdf4-tools liblcms2-utils odbc-postgresql tdsodbc ogdi-bin poppler-utils
ghostscript fonts-japanese-mincho | fonts-ipafont-mincho fonts-japanese-gothic | fonts-ipafont-gothic fonts-arphic-ukai
fonts-arphic-uming fonts-nanum
The following NEW packages will be installed:
gdal-data gdal-plugins libaec0 libarmadillo12 libarpack2t64 libblas3 libblosc1 libboost-serialization1.83.0 libcfitsio10t64 libfreexl1
libfyba0t64 libgdal34t64 libgeos-c1t64 libgeos3.12.1t64 libgeotiff5 libgfortran5 libgif7 libgmpxx4ldbl libhdf4-0-alt libhdf5-103-1t64
libhdf5-hl-100t64 libkmlbase1t64 libkmldom1t64 libkmlengine1t64 liblapack3 liblcms2-2 libltdl7 libminizip1t64 libmysqlclient21
libnetcdf19t64 libodbc2 libodbcinst2 libogdi4.1 libopenjp2-7 libpoppler134 libproj25 libqhull-r8.0 librttopo1 libsfcgal1t64 libsnappy1v5
libspatialite8t64 libsuperlu6 libsz2 liburiparser1 libxerces-c3.2t64 mysql-common poppler-data postgis postgis-doc
postgresql-16-postgis-3 postgresql-16-postgis-3-scripts postgresql-postgis postgresql-postgis-scripts proj-bin proj-data unixodbc-common
0 upgraded, 56 newly installed, 0 to remove and 2 not upgraded.
Need to get 45.9 MB of archives.
After this operation, 200 MB of additional disk space will be used.
Do you want to continue? [Y/n]
Execute psql command and connect to the database. In this case, gis database
sudo -u postgres psql
\c gis
Enable PostGIS extension
CREATE EXTENSION postgis;
Output:
dhani@ubuntu-postgis:~$ sudo -u postgres psql
psql (16.6 (Ubuntu 16.6-0ubuntu0.24.04.1))
Type "help" for help.
postgres=# \c gis
You are now connected to database "gis" as user "postgres".
postgres=# create extension postgis;
CREATE EXTENSION
Now PostGIS extension is enabled in our database. Last think we need to do is enable the remote connection to our database. By enabling this feature, user in the network can connect to the database.
Step 5. Enable Remote Connection to PostgreSQL
Edit postgresql.conf
We need to edit the postgresql.conf file. We will use nano to edit this file.
sudo nano /etc/postgresql/16/main/postgresql.conf
Now scroll down and find this entry
#listen_addresses = ‘localhost’
Edit the line and change it to:
listen_addresses = ‘*’
This will allows connection from any IP address. You can use your own IP address block if you want to. Close (CTRL+X) and save it.
Edit pg_hba.conf
Lets edit the pg_hba.conf file. Use the command below
sudo nano /etc/postgresql/16/main/pg_hba.conf
Now find the IPv4 local connection section at the bottom of the file
#IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
Change it to
host all all 0.0.0.0/0 scram-sha-256
Close and save the editor. Now restart postgresql service
sudo systemctl restart postgresql
Now PostgreSQL and PostGIS is accessible from the network.
Pingback: How to Export Layer to PostGIS in QGIS | geodatainsights.com