Displaying a map in a Django Webapp (1/3): Creating a GIS database with PostgreSQL and PostGIS

Hakim Benoudjit
3 min readApr 8, 2018

--

To avoid making this tutorial too long to read and follow, I’ve preferred to split it into three parts:

  1. Create a GIS database with PostgreSQL and PostGIS (this tutorial).
  2. Develop a GIS webapp with GeoDjango.
  3. Showing a map on the Django admin.

In the following I’m going to demonstrate how I’ve installed PostgreSQL (on both Windows and Arch Linux) and created a new geospatial database with PostGIS.

On Windows

The installation of PostgreSQL and the configuration of PostGIS is quite straightforward, compared to how it’s done on Arch Linux (in the next section).

The link to the latest version of PostgreSQL (version 10 as of when this tutorial was posted) can be found on the official website, and can be downloaded from EntrepriseDB’s website.

At the end of the installation, you’ll be asked whether you want to use the stack builder to install additional tools:

And that’s when we’ll install PostGIS which is the de facto geospatial extension for PostgreSQL:

During the installation of PostGIS, there is a possibility to create a geo-database without having to do it from the command-line like for Arch Linux. All you have to do is to make sure the checkbox “Create a spatial database” below is checked. I’ve called the database “<my-database>” in what follows:

To have access to the PostgreSQL command-line client (psql) later, we need to add the path to it in PostgresSQL’s folder to the Path system variable. For me the path that was added is “C:\Program Files\PostgreSQL\10\bin” (don’t forget the semi-colon to separate between the different paths):

On Arch Linux

More details can be found on PostgreSQL’s page on Arch Linux website. In a nutshell, we install PostgreSQL, PostGIS and PgAdmin (GUI administration tool):

Then, the database must be initialized, as this is not automatic like on Windows:

And the PostgreSQL service has to be started and enabled to run on boot:

For both

Regardless of whether you’re on Windows or Linux, the next procedure to create a user with SQL from the command-line will be the same. So, you need to run the PostgreSQL client from the command-line. Note that we are connecting to the database server using the “postgres” user, which was created automatically during the installation of PostgreSQL:

Create the database user that will be used by Django to access the database:

On Windows, the database already exists but needs to be owned by the new user just created, so that he can have all the privileges when accessing it later from Django. This will be the last instruction required on Windows; the rest of the tutorial will focus on Arch Linux:

On Arch Linux, we need to create a GIS database (as has already been done on Windows but ‘manually’) and specify the previous user as its owner:

Always on Arch Linux, we exit the psql prompt with “\q”, and connect to this new database with the newly-created user:

Then, we enable the PostGIS extension on this database on Arch Linux:

Now that the GIS database is created with its user, we can consider this part of the tutorial done. In the next step, we will see how to develop a GIS web app with GeoDjango that connects to the database that has been created.

Don’t forget to clap if this story has been useful to you.

--

--