How to Install PostGIS Extension for PostgreSQL on Ubuntu

PostgreSQ is a free open source database with extensions available to extend its capabilities. PostGIS is a spatial database extension that provides PostgreSQL with location and geography capabilities. PostGIS adds a POINT datatype for location and can return location radii and can return location radiuses and distances using relevant queries

Configure a repository

Install the required Gnu Privacy Guard dependency

$ sudo apt -y install gnupg2

Import the PGP signing key for the given repository

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

Add PostgreSQL apt repository

$ echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list

Install PostgreSQL and PostGIS

In this step, you must first install PostgreSQL

$ sudo apt -y install postgresql-12 postgresql-client-12

Then install PostGIS

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

After that, install all the necessary control packages

$ sudo apt-get install postgresql-12-postgis-3-scripts

Log into PostgreSQL and enable PostGIS

First, log in as superuser

$ sudo su - postgres

Next, create a new PostgreSQL user

$ createuser exampleuser

After that, set a password for the new user

$ psql -c "alter user exampleuser with password 'yourPassword'"

Create a new database

$ createdb my_db -O exampleuser

Connect to database

$ psql -d my_db

Enable PostGIS Database Extension

my_db=# CREATE EXTENSION postgis;

Test the PostGIS extension

my_db=# SELECT PostGIS_version();

Was this article helpful?

Related Articles

Leave A Comment?