DEV Community

Hasanul Islam
Hasanul Islam

Posted on • Updated on

GeoDjango Installation: PostGIS and GDAL

GeoDjango is a world-class geographic Web framework. We will install GeoDjango step by step:

Installing Geospatial library: GDAL :

GeoDjango provides interfaces for some source geospatial libraries e.g. GEOS, PROJ.4, GDAL, GeoIP, PostGIS, SpatiaLite. On ubuntu/Debian, We will install the following packages which will install, directly or by dependency, the required geospatial libraries:

sudo apt-get update
sudo apt-get install python3-pip python3-dev libpq-dev 
sudo apt-get install binutils libproj-dev gdal-bin
pip install GDAL
Enter fullscreen mode Exit fullscreen mode

Database installation: PostGIS :

PostGIS adds geographic object support to PostgreSQL, turning it into a spatial database.

Install PostgreSQL :

sudo apt-get update
sudo apt install postgresql postgresql-contrib
Enter fullscreen mode Exit fullscreen mode

Create Database, User and Grant Privileges :

Log into an interactive Postgres session by typing:

sudo -u postgres psql
Enter fullscreen mode Exit fullscreen mode

First, create a database.

postgres=# CREATE DATABASE db_name;
Enter fullscreen mode Exit fullscreen mode

Next, create a database user.

postgres=# CREATE USER db_user WITH PASSWORD 'password';
Enter fullscreen mode Exit fullscreen mode

Then, give our new user access to administer our new database:

postgres=# GRANT ALL PRIVILEGES ON DATABASE db_name TO db_user;
Enter fullscreen mode Exit fullscreen mode

Exit out of the PostgreSQL prompt by typing:

postgres# \q
Enter fullscreen mode Exit fullscreen mode

Install PostGIS apt package :

sudo apt install postgis --no-install-recommends
Enter fullscreen mode Exit fullscreen mode

Install the package that contains postgis.control manually :

sudo apt-get install postgis postgresql-<POSTGRESQL_VERSION>-postgis-scripts
Enter fullscreen mode Exit fullscreen mode

Create PostGIS Extension :

sudo -u postgres psql
postgres=# \c db_name
db_name=# CREATE EXTENSION postgis;
Enter fullscreen mode Exit fullscreen mode

DATABASES configuration :ΒΆ

Add the following ENGINE to DATABASES config at settings.py:

DATABASES = {
    'default': {
         'ENGINE': 'django.contrib.gis.db.backends.postgis',
          ....
    },
}
Enter fullscreen mode Exit fullscreen mode

Add django.contrib.gis to INSTALLED_APPS :

INSTALLED_APPS = [
    'django.contrib.admin',
     .......,
    'django.contrib.gis',
]
Enter fullscreen mode Exit fullscreen mode

Feel free to leave a comment.

Top comments (3)

Collapse
 
borsemayur2 profile image
Mayur Borse

I'm getting error:
could not open extension control file "/usr/share/postgresql/12/extension/postgis.control"

Collapse
 
borsemayur2 profile image
Mayur Borse

Resolved by using command

sudo apt-get install postgis postgresql-<POSTGRESQL_VERSION>-postgis-scripts

Enter fullscreen mode Exit fullscreen mode
Collapse
 
mhihasan profile image
Hasanul Islam

Thanks