Chris B.
Chris B.

Reputation: 1575

NameError: name 'DATABASES' is not defined with AWS RDS

Trying to get an AWS RDS configured with a Heroku app. I've used postgres before but this is my first time with RDS. I followed their docs here (https://devcenter.heroku.com/articles/amazon_rds) but when I attempt to run a syncdb or access the dbshell from Heroku I get the following error:

$ heroku run ./manage.py syncdb
Running `./manage.py syncdb` attached to terminal... up, run.8947
Traceback (most recent call last):
 File "./manage.py", line 10, in <module>
   execute_from_command_line(sys.argv)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 263, in fetch_command
app_name = get_commands()[subcommand]
 File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 109, in get_commands
apps = settings.INSTALLED_APPS
 File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
self._wrapped = Settings(settings_module)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 132, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
 File "/app/.heroku/python/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
 File "/app/rfp/settings.py", line 33, in <module>
'amazon-rds-ca-cert.pem')}
NameError: name 'DATABASES' is not defined

ok, so looks to be a db config issue with settings. Here's my relevant settings.py

import os
import sys
import dj_database_url

if DEBUG:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_ROOT, 'db.sqlite3'),             
    }
}

else:
     dbconfig = dj_database_url.config()

     if dbconfig:
         DATABASES['default']['OPTIONS'] = {
             'ssl': {'ca': os.path.join(os.path.dirname(__file__), 'etc', 'amazon-rds-ca-cert.pem')}
    }  

Then in heroku I've set config vars for DATABASE_URL and DEBUG:

DATABASE_URL:          mysql2://db_user:db_pw@db_server/db?sslca=config/amazon-rds-ca-cert.pem
DEBUG:                 False

The .pem file is in an 'etc' folder in the app root. Anyone see a step I've missed or misconfigured in setting this up? thanks

Upvotes: 0

Views: 1258

Answers (1)

Alex
Alex

Reputation: 8539

Your main problem is that you never set your DATABASES when DEBUG = False. You set a variable dbconfig to your database settings, but you never made that your default database. You need something like:

else:
     dbconfig = dj_database_url.config()

     if dbconfig:
         DATABASES = {'default': dj_database_url.config()} # Add this
         DATABASES['default']['OPTIONS'] = {
             'ssl': {'ca': os.path.join(os.path.dirname(__file__), 'etc', 'amazon-rds-ca-cert.pem')}
    } 

Once you've set your default database, then you can edit the OPTIONS on it.

EDIT:

Also, for what it's worth, this answer said you can't use python to construct the SSL path. You need to hard code it.

Upvotes: 0

Related Questions