About Creating Spatial Postgres Database

To use spatial features like distance calculation between points with the Postgres database, you need PostGIS extension. It has to be activated for each Postgres database individually. On MacOS, you can install the database management system and extension with Homebrew:

1
2
3
$ brew install postgres
$ brew install postgis
$ brew services start postgres

Create the user and database for a new project and activate the PostGIS extension with these commands:

1
2
3
4
$ createuser --createdb --password myprojectuser
$ createdb --username=myprojectuser myprojectdb
$ psql myprojectdb
> CREATE EXTENSION postgis;

Then use "django.contrib.gis.db.backends.postgis" as the database backend in your Django project settings:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
DATABASES = {
    "default": {
        "ENGINE": "django.contrib.gis.db.backends.postgis",
        "NAME": "myprojectdb",
        "USER": "myprojectuser",
        "PASSWORD": "secretpassword",
        "HOST": "127.0.0.1",
        "PORT": "5432",
    }
}

Tips and Tricks Programming Dev Ops Databases Django 5.x Django 4.2 Django 3.2 PostgreSQL PostGIS