Article

Table of Contents
Theme:
Was this article helpful?

1  out of  1 found this helpful

Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $300 by adding new articles!

Install the PostGIS Extension for PostgreSQL on Ubuntu Linux

Last Updated: Mon, Aug 10, 2020
PostgreSQL Ubuntu

Introduction

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.

Prerequisites

1. Set up Repository

Add the PostgreSQL repository to your system. This repository provides automatic updates for all supported versions of PostgreSQL throughout the supported lifetime of PostgreSQL.

  1. Install the required Gnu Privacy Guard dependency.

    $ sudo apt -y install gnupg2
    
  2. Import the repository PGP signing key.

    $ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
    
  3. 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
    

2. Install PostgreSQL and PostGIS

PostgreSQL Version 12

Follow these steps if using PostgreSQL version 12.

  1. Install PostgreSQL.

    $ sudo apt -y install postgresql-12 postgresql-client-12
    
  2. Install PostGIS.

    $ sudo apt install postgis postgresql-12-postgis-3
    
  3. Install the required control packages.

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

PostgreSQL Version 11

Follow these steps if using PostgreSQL version 11.

  1. Install PostgreSQL.

    $ sudo apt -y install postgresql-11
    
  2. Install PostGIS.

    $ sudo apt install postgis postgresql-11-postgis-3
    
  3. Install the required control packages.

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

3. Log into PostgreSQL and Enable PostGIS

  1. Log in as the superuser.

    $ sudo su - postgres
    
  2. Create a new PostgreSQL user.

    $ createuser exampleuser
    
  3. Set the password for the new user.

    $ psql -c "alter user exampleuser with password 'yourPassword'"
    
  4. Create a new database.

    $ createdb my_db -O exampleuser
    
  5. Connect to the database.

    $ psql -d my_db
    
  6. Enable the PostGIS extension for the database.

    my_db=# CREATE EXTENSION postgis;
    
  7. Test the PostGIS extension.

    my_db=# SELECT PostGIS_version();
    

Conclusion

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.

Want to contribute?

You could earn up to $300 by adding new articles