80

I'm attempting to run heroku run python manage.py syncdb on my GeoDjango app on Heroku, but I get the following error:

AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

All of my research has yielded the same solution: make sure to use django.contrib.gis.db.backends.postgis as the database engine. Funny thing is that I'm already doing this (and I also have django.contrib.gis in INSTALLED_APPS):

settings.py

DATABASES = {
  'default': {
    'ENGINE': 'django.contrib.gis.db.backends.postgis',
    'NAME': '...',
    'HOST': '...',
    'PORT': ...,
    'USER': '...',
    'PASSWORD': '...'
  }
}

INSTALLED_APPS = (
    ...,
    'django.contrib.gis',
)

Is there something else I am missing? Any help is greatly appreciated, below is the full error trace for reference:

Running `python manage.py syncdb` attached to terminal... up, run.1
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table django_admin_log
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/app/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/app/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/app/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 91, in handle_noargs
    sql, references = connection.creation.sql_create_model(model, self.style, seen_models)
  File "/app/lib/python2.7/site-packages/django/db/backends/creation.py", line 44, in sql_create_model
    col_type = f.db_type(connection=self.connection)
  File "/app/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.py", line 200, in db_type
    return connection.ops.geo_db_type(self)
AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type'

Update: I followed the GeoDjango tutorial and Heroku/Django tutorial, and built a simple app that works on my dev machine. I pushed it to Heroku using a custom GeoDjango buildpack, and tried syncdb, but get the same error. Is this an issue with Django/GeoDjango, Heroku, or the buildpack? My dev environment is using PostgreSQL 9.1 and PostGIS 2.0, but Heroku uses 9.0.9 and 1.5, could that be the issue?

10 Answers 10

100

The OP was using the GeoDjango buildpack, but in case anyone gets here using Geo buildpack and dj_database_url like I was, in settings.py don't forget the last line:

import dj_database_url
DATABASES['default'] = dj_database_url.config()
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'

UPDATE

dj_database_url directly supports PostGIS. You can do without the last line in the code above if you can change your database URL to start with postgis.

4
  • 1
    With setting issue can be fixed. While running the Django server it will cause an issue. Below @Sillson approach will solve the issue. Issue Encounterd:: Traceback (most recent call last): File "/Users/seenu/.venv/1729/lib/python3.7/site-packages/django/template/utils.py", line 66, in __getitem__ return self._engines[alias] KeyError: 'django'
    – Seenu S
    Commented Mar 28, 2019 at 5:50
  • Saved me, I guess, ton of time, because I'm not sure how would I find this reason. But strangely all seemed to work yesterday and I was surprised to see error today. IDK, maybe it was a dream. Thank you!
    – Denis
    Commented Jan 8, 2021 at 20:21
  • Now I am getting this error please help django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the NAME value.
    – Ali Husham
    Commented Nov 26, 2021 at 10:17
  • for dokku, before linking use dokku config:set POSTGRES_DATABASE_SCHEME=postgis
    – Udi
    Commented Apr 2, 2023 at 12:36
41

I got this error when trying to run tests with the test db set like so:

if 'test' in sys.argv:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3', 
            'NAME': '_testdb',
        }
    }

The problem is that the sqlite3 DatabaseOperations object doesn't have the attribute geo_db_type (like the title of this post suggests).

My solution was to change the backend to the sqlite equivalent GIS engine:

        'ENGINE': 'django.contrib.gis.db.backends.spatialite'

See the Django docs on GeoDjango installation for all the possible backends, with installation instructions: https://docs.djangoproject.com/en/3.0/ref/contrib/gis/install/#spatial-database

2
  • The link seems to be dead. Could you maybe post a new link AND copy the relevant text to your answer? Thank you!
    – ThaJay
    Commented Feb 5, 2020 at 16:15
  • Updated link. Looks like v1.9 has been dead for awhile.
    – bozdoz
    Commented Feb 5, 2020 at 16:35
29

I was having the same problem and I had to change:

'ENGINE': 'django.db.backends.postgresql_psycopg2',

to:

'ENGINE': 'django.contrib.gis.db.backends.postgis',
19

for me helped

1) add 'django.contrib.gis', to INSTALLED_APPS
2) change from

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',

to

DATABASES = {
'default': {
    'ENGINE': 'django.contrib.gis.db.backends.mysql', 
13

This post is old but I just wanted to share my answer to this problem. I'm using the Dj Database package, and I didn't know that the connection URL is different when using PostGIS. The connection string for PostGIS is postgis://USER:PASSWORD@HOST:PORT/NAME

Hope this helps someone.

3
  • Actually this change on the URL save me the day! Commented Jun 27, 2018 at 14:33
  • Worked like a charm!
    – Kurt Peek
    Commented Sep 5, 2018 at 4:21
  • 1
    what is connectoin string mean? where should i put it?
    – Ali Husham
    Commented Nov 26, 2021 at 10:14
10

I'm using the Python sample application on stack cedar 14 and the regular Heroku buildpack Heroku/python with PostGIS and had the same problem that my database settings were overwritten with the wrong DB engine, which caused heroku run python manage.py migrate to fail with the above error. Just adding the engine in the settings would not change anything. After some investigation I found out that it is the call to django_heroku.settings(locals()) in the last line of my settings.py which is reverting my changes.

I fixed it by overwriting the engine again like this by adding a line afterwards:

django_heroku.settings(locals())
DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'
4
  • This totally fixed this for me. I feel like this should be brought up as an issue with django_heroku, as I don't think this is intended functionality.
    – Todd
    Commented Jul 29, 2020 at 20:22
  • 1
    Excellent answer. This fixed my problem. For what it's worth django_heroku (and the newer django_on_heroku) now both accept a geodjango=True kwarg to avoid this probem [github.com/pkrefta/django-on-heroku#enabling-functionality] Commented Mar 16, 2022 at 23:09
  • Thank you so much. Now i also understood, what django_heroku really does.
    – Irfan wani
    Commented May 8, 2022 at 13:50
  • thanks this solved my issue spent 4-5 hours trying to solve this Commented Oct 17, 2022 at 12:16
5

I changed my default db engine from psycopg2 to postgis

PREVIOUSLY

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        ...,
    }
}

NOW

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        ...,
    }
}
1

Settings.py > INSTALLED_APPS

INSTALLED_APPS = [
.
.
'django.contrib.gis'
.
]

Settings.py > Databases

DATABASES = {
'default': {
    'ENGINE': 'django.contrib.gis.db.backends.mysql',
     ...,
    }
} 
-1

In may case I was having this issue because I forgot to comment out the db settings further down in settings.py:

# Update database configuration with $DATABASE_URL.
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

These lines were overriding the settings that I had added above. Adding this in case someone else had the same issue.

-2

The buildpack was the primary culprit here. Instead of using the GeoDjango buildpack listed on Heroku's buildpack page, I used one of it's forks that was updated more recently.

Also, when I do a git push heroku master, Heroku creates a dev database for the app, and when I do a syncdb, my DATABASES setting is ignored and Heroku tries to use the dev database instead... obviously an issue, because dev databases don't/can't have PostGIS installed. So I destroyed the dev database after it was created with the git push (with the correct buildpack), then ran syncdb and it works.

2
  • i am still getting this error after using the custom build pack.
    – limovala
    Commented May 26, 2013 at 8:03
  • 6
    While the question is Heroku specific, the title is quite general. The problem can also arise if Django DATABASES ENGINE is not set to django.contrib.gis.db.backends.postgis Commented Sep 22, 2013 at 19:34

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.