Installing PostGIS for Postgresql on Ubuntu/Mint — GeoDjango

Dec. 11, 2019, 10:36 a.m.

Working on my hobby pet bicycle routes project I decided to try full strength of GeoDjango functionality. I already have Postgresql database there so extension should be created for it. Firstly let’s install all required packages. I’m using version 9.3 of PostgreSQL so package names could vary in your case:

sudo apt install postgis postgresql-9.3-postgis-scripts

Multiple packages are required to make PostGIS working with PostgreSQL:

After packages are installed you need to create extension for your database. Let’s say we have “my_lovely_db” database we want to have PostGIS extension for.

Run psql as postgres user

sudo -u postgres psql

Connect to database and create extension

\connect my_lovely_db
CREATE EXTENSION postgis;

After installation check current version

SELECT PostGIS_version();
            postgis_version            
---------------------------------------
 2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1
(1 row)

Congratulations! You have PostGIS extension created! Now we can start working with GeoDjango contrib module!

Possible errors

ERROR: could not open extension control file “/usr/share/postgresql/9.3/extension/postgis.control”: No such file or directory

Means there is no appropriate extension installed. Check you Postgresql version and install postgresql-X.X-postgis-scripts package. It was postgresql-9.3-postgis-scripts in my case.

ERROR: Could not access the file «$libdir/postgis-2.1»: No such file or directory

This one could happen or could not. Depending on available repositories and version of Postgresql. I had it with 9.5 version on my local machine and it was requestion $libdir/postgis-2.2. Install this package:

sudo apt install postgresql-X.X-postgis-Y.Y

Enjoy!