PostGIS is a spatial extension for PostgreSQL. If you own PostgreSQL Server you can use it to store your spatial databases such as polygon, polyline or points in the PostgreSQL server. By default, PostGIS is not enabled in PostgreSQL. On this tutorial, we are going to learn how to install PostGIS on Fedora 28. There are several options to install PostGIS and PostgreSQL on Fedora 28.
- Install PostgreSQL and PostGIS on the Fedora host
- Run PostgreSQL and PostGIS container using Docker
I am not going to explain the installation on the Fedora host. There are many tutorials that explain this already. Here we will deploy a PostgreSQL server container and then install PostGIS on top of it. There are several steps we need to take in order to have a fully working PostGIS spatial database server using Docker container.
- Install Docker CE on Fedora 28
- Run PostgreSQL container
- Install PostGIS
- Create a new database
- Enable PostGIS extension on existing database
Step 1. Install Docker on Fedora 28
Please follow the instruction on this page to get Docker CE up and running on Fedora 28. It’s a pretty simple tutorial that is easy to follow.
Step 2. Run PostgreSQL Container
After Docker is up and running, let’s fire up the PostgreSQL container with the following command
docker run --name postgresql01 --restart=always -e POSTGRES_PASSWORD=12345 -p 54320:5432 -d postgres
Change the following items with your own settings:
- postgresql01 ==> name of the container. You can change it to anything you like
- 12345 ==> password for postgres user.
- 54320 ==> assigned port. The default port is 5432. I am assigning port 54320 on my Fedora host. You can either use the default port or change to something else. If you use default port, change it to 5432:5432
- postgres ==> The image used to deploy the server. It will download the latest version from docker hub.
When complete, check with docker ps command, and make sure our container is up and running.
So at this point, we have a working PostgreSQL Server.
Step 3. Install PostGIS inside the PostgreSQL container
Next, we are going to install PostGIS inside the container we created on the previous step. We are going to enter the container shell.
docker exec --it postgresql01 /bin/bash
Depends on how you name your container, change postgresql01 with the actual container name. Now, while in the container shell, execute the update and install postgis
root@039690da9321:/# apt update && apt install postgis
It will install a bunch of dependency files.
Login to PostgreSQL Server
Once installation completes, let’s continue to create a new database. Still inside the container let’s login to PostgreSQL server with user postgres.
psql -U postgres
root@039690da9321:/# psql -U postgres psql (10.5 (Debian 10.5-1.pgdg90+1)) Type "help" for help. postgres=#
Create a New Database
So now, we are inside the PostgreSQL console. Let’s create a new database called gis_database
create database gis_database;
postgres=# create database gis_database; CREATE DATABASE
Enable PostGIS Extension on a Database
Now we have a new database. Next, we need to enable the postgis extension on the new database. For your information, postgis extension needs to be enabled on every database you want to use it as a spatial database.
First, connect to the database
postgres-# \c gis_database; You are now connected to database "gis_database" as user "postgres". gis_database-#
Now let’s enable the postgis extension to our gis_database
CREATE EXTENSION postgis;
gis_database=# CREATE EXTENSION postgis; CREATE EXTENSION
That’s it. Now we have a new PostGIS database called gis_database and we can start uploading our spatial datasets to this new PostGIS database.