Lev
Lev

Reputation: 1009

unable to make RDS queries on heroku

I can access my RDS postresql database on a local machine no problem.

settings.py

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',
    'NAME': 'xxxxxxx',
    'USER': 'XXXXXXXX',
    'PASSWORD': 'XXXXXXXX',
    'HOST': 'XXXXXXrds.amazonaws.com',
    'PORT': '5432',
}

I pushed this to Heroku and I get a ProgrammingError at /saferdb/query/

In manage.py shell on heroku I tried to access the database:

>>> from saferdb.models import Question
>>> q = Question.objects.all()
>>> q.count()

got the following error:

Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "saferdb_question" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "saferdb_question"
                                          ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 387, in count
    return self.query.get_count(using=self.db)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 491, in get_count
    number = obj.get_aggregation(using, ['__count'])['__count']
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py", line 476, in get_aggregation
    result = compiler.execute_sql(SINGLE)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1063, in execute_sql
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "saferdb_question" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "saferdb_question"
                                          ^

Upvotes: 3

Views: 1313

Answers (2)

Milan Cermak
Milan Cermak

Reputation: 8064

I had a similar issue, also for a Django on Heroku app. I used the heroku-django-template to create my app. In settings.py, the default database is overwritten by the environment variable DATABASE_URL.

You can either disable that in the settings.py or set the env var to the correct value.

Upvotes: 1

Sakthi Panneerselvam
Sakthi Panneerselvam

Reputation: 1397

The Error is because of table doesn't exists in Database, that means you need to migrate the database models. For Further reference read django migrations here

Database migrations in heroku is done by this way, specify the line in Procfile file.

release: python manage.py migrate
web: gunicorn col.wsgi --log-file -

So Whenever you deploy a new code, heroku will automatically migrate the DB models. For further reference, check here. let me know if this works.

Upvotes: 2

Related Questions