Kamal Hosen
Kamal Hosen
Geospatial Developer | Data Science | Python

Nov 07, 2022

How to Install PostgreSQL with PostGIS in Linux Ubuntu?

/media/article-img/install-postgis.png

What is PostgreSQL?

PostgreSQL is one of the leading powerful, robust, open-source object-relational database systems. It is used as a data warehouse for storing data for mobile, web applications, and others. PostgreSQL supports both SQL (relational) and JSON (non-relational) querying. PostgreSQL has been actively being developed for more than 30 years which makes it a strong, reliable, and stable database. It supports most of the popular programming languages, such as Python, Java, C#, C/C+, Ruby, Perl, JavaScript, Go, and so on. It is primarily used as a robust back-end database that powers many dynamic websites and web applications.

 

What is PostGIS?

PostGIS is a powerful open-source spatial database extension of PostgreSQL to create and store Geographic objects on the PostgreSQL object-relational database. Thanks, Open Geospatial Consortium (OGC), for bringing this powerful spatial database to solve the complex real-world data storing and retrieving. The features of PostGIS are derived from Open Geospatial Consortium SQL specifications.

PostGIS series provides:

  • Processing and analytic functions for both vector and raster data for slicing, dicing, morphing, reclassifying, and collecting/uniting with the power of SQL

  • Raster map algebra for fine-grained raster processing

  • Spatial reprojection SQL callable functions for both vector and raster data, etc. Commonly, PostGIS is used by Open Source Mapping Servers. Also, it conforms to OpenGIS Consortium (OGC) mapping standards like WMS (Web Map Service), WFS (Web Feature Service), WFS-T (Web Feature Service Transactional (ability to edit)), WCS (Web coverage service), WPS (Web Processing Service), WMTS (Web Map Tile Service). 

  • Data can be extracted from the PostGIS database using SQL

  • It supports routing through pgRouting and road datasets

 

In this tutorial, I will show you how to install PostgreSQL and PostGIS on Linux Ubuntu Machine.

 

Step 1: Update the system

sudo apt update
sudo apt -y upgrade

 

Step 2: Add the PostgreSQL repository

To install the latest version of PostgreSQL, we will first add PostgreSQL APT repository to our Ubuntu system.

# 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


Step 3: Install PostgreSQL

The PostgreSQL repository contains different versions of PostgreSQL. We can define the specific version of PostgreSQL or the latest version during the installation.

# Install the latest version of PostgreSQL.
sudo apt-get -y install postgresql

If you want a specific version, For example of, installing PostgreSQL version 12, use 'PostgreSQL-12' or similar instead of 'PostgreSQL':

sudo apt-get install postgresql-12

Replace the version number with your desired installation version for different versions of PostgreSQL.

 

Step 4: Install PostGIS

After the successful installation of PostgreSQL, we are now ready to install PostGIS.

We have installed PostgreSQL's latest version, which is 15 now.

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

 

If you want to install PostGIS for another version, make sure you replace the postgresql version with your postgresql installation version. For an example of PostGIS for postgresql 12, the script should be -

sudo apt install postgis postgresql-12-postgis-3

 

Step 5: Enable PostGIS

When you install PostgreSQL, it will create a default database, postgres, with username postgres.

You can access the default database, but you cannot store spatial data in that database because, by default, postgis is not enabled. You need to enable postgis first; then, you can store geographic objects.

To enable postgis on the default database, first login to the database -

sudo -i -u postgres psql

Now create an extension.

CREATE EXTENSION postgis;

Now, verify the postgis installation;

SELECT PostGIS_version();

 

Quit postgresql by typing \q and hit enter.

 

Share To

About Author
  • Kamal Hosen
  • Kamal Hosen
    Geospatial Developer | Data Science | Python

    A passionate geospatial developer and analyst whose core interest is developing geospatial products/services to support the decision-making process in climate change and disaster risk reduction, spatial planning process, natural resources management, and land management sectors. I love learning and working with open source technologies like Python, Django, LeafletJS, PostGIS, GeoServer, and Google Earth Engine.