PostgreSQL is a free open source object-relational database system with extensions available to extend its capabilities. PostGIS is a spatial database extension that provides location and geographic capabilities for PostgreSQL. PostGIS adds the POINT
datatype to define locations and can return location radiuses and distances using relevant queries.
This installation guide covers PostgreSQL versions 11 and 12.
Add the PostgreSQL repository to your system. This repository provides automatic updates for all supported versions of PostgreSQL throughout the supported lifetime of PostgreSQL.
Install the required Gnu Privacy Guard dependency.
$ sudo apt -y install gnupg2
Import the repository PGP signing key.
$ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
Add the 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
Follow these steps if using PostgreSQL version 12.
Install PostgreSQL.
$ sudo apt -y install postgresql-12 postgresql-client-12
Install PostGIS.
$ sudo apt install postgis postgresql-12-postgis-3
Install the required control packages.
$ sudo apt-get install postgresql-12-postgis-3-scripts
Follow these steps if using PostgreSQL version 11.
Install PostgreSQL.
$ sudo apt -y install postgresql-11
Install PostGIS.
$ sudo apt install postgis postgresql-11-postgis-3
Install the required control packages.
$ sudo apt-get install postgresql-11-postgis-3-scripts
Log in as the superuser.
$ sudo su - postgres
Create a new PostgreSQL user.
$ createuser exampleuser
Set the password for the new user.
$ psql -c "alter user exampleuser with password 'yourPassword'"
Create a new database.
$ createdb my_db -O exampleuser
Connect to the database.
$ psql -d my_db
Enable the PostGIS extension for the database.
my_db=# CREATE EXTENSION postgis;
Test the PostGIS extension.
my_db=# SELECT PostGIS_version();
After installing the PostGIS extension, you can create fields that store geometry types. The geometrical data can be accessed using SQL queries. See the PostGIS documentation for more details and examples.