Help with GeoDjango / PostGIS database setup


I’m building a GeoDjango app but can’t get my server to connect the Postgres database using the PostGIS driver.

I created my app following following the Django getting started guide and opted to automatically create a Postgres database.
I found this this post which seems to imply that PostGIS has been compiled into the Postgres image for awhile.

I can build and run the docker image on my computer, and successfully connect it to a local postgis/postgis:14-3.3 docker container successfully.

When I fly deploy and ssh into the machine and run python migrate I get a database connection error:

django.db.utils.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

I tried changing the default database ENGINE from django.contrib.gis.db.backends.postgis back to postgresql, which seemed to confirm that this is the issue, as it got further into the migration, but bailed on AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

Does anyone know what I need to do to get PostGIS up? Can I use the built in postgres database functionality or do I need to deploy PostGIS from the image?

Thanks in advance.

1 Like

Ok, I got this to work. What I did:

  1. fly pg connect and run all the CREATE EXTENSION commands I hadn’t run in the PostGIS install instructions
  2. The initial CREATE EXTENSION statement crashed the DB due to OOM, so I ran fly pg restart and tried step 1 again, which worked
  3. migrate from my app, this still crashed with a bad driver error
  4. fly pg restart again (just in case)
  5. migrate then worked

I’m not 100% why the restarts were necessary, so I would still welcome any wisdom – but it’s working.