pythondjangoconfigparsersyncdb

Running syncdb in django after converting to ConfigParser


So I recently converted my django database settings to use a settings.ini file per the guide found here

My issue is that now syncdb has stopped working for new deployments:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/usr/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/usr/lib/python2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 15, in complain
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. "
django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

settings.ini:

[database]
DATABASE_ENGINE: django.db.backends.postgresql_psycopg2
DATABASE_NAME: postgres
DATABASE_USER: postgres
DATABASE_PASSWORD:
DATABASE_HOST: db
DATABASE_PORT: 5432

settings.py:

import os
import sys
from ConfigParser import RawConfigParser

config = RawConfigParser()
config.read('settings.ini')

DEBUG = bool(os.environ.get('DEBUG', False))
DEBUG_TOOLBAR = bool(os.environ.get('DEBUG_TOOLBAR', False))
TEMPLATE_DEBUG = bool(os.environ.get('TEMPLATE_DEBUG', False))

#Database Config is pulled from the config file
DATABASE_USER = config.get('database', 'DATABASE_USER')
DATABASE_PASSWORD = config.get('database', 'DATABASE_PASSWORD')
DATABASE_HOST = config.get('database', 'DATABASE_HOST')
DATABASE_PORT = config.get('database', 'DATABASE_PORT')
DATABASE_ENGINE = config.get('database', 'DATABASE_ENGINE')
DATABASE_NAME = config.get('database', 'DATABASE_NAME')

I originally started going down this path so our service can be run at different sites with an easy to config manage file for the settings. It also seeks to make dev environments breezy since the config no longer has to be in the codebase since it can be stored elsewhere.

Have I implemented something incorrectly or have I hit a catch 22 where this type of setting change only works if the db is already synced?

This is in Django 1.6.5


Edit

I forgot to mention that python manage.py diffsettings returns the expected values, so I know manage.py can access and interperet the settings.ini file correctly.

DATABASE_ENGINE = 'django.db.backends.postgresql_psycopg2'  ###
DATABASE_HOST = 'db'  ###
DATABASE_NAME = 'postgres'  ###
DATABASE_PASSWORD = ''  ###
DATABASE_PORT = '5432'  ###
DATABASE_USER = 'postgres'  ###

Solution

  • Django's database configuration is in format e.g.:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'mydatabase',
            'USER': 'mydatabaseuser',
            'PASSWORD': 'mypassword',
            'HOST': '127.0.0.1',
            'PORT': '5432',
        }
    }
    

    Check docs for details.

    So you need something like this in your settings.py:

    DATABASES = {'default':{}}
    #Database Config is pulled from the config file
    DATABASES['default']['USER'] = config.get('database', 'DATABASE_USER')
    DATABASES['default']['PASSWORD'] = config.get('database', 'DATABASE_PASSWORD')
    DATABASES['default']['HOST'] = config.get('database', 'DATABASE_HOST')
    DATABASES['default']['PORT'] = config.get('database', 'DATABASE_PORT')
    DATABASES['default']['ENGINE'] = config.get('database', 'DATABASE_ENGINE')
    DATABASES['default']['NAME'] = config.get('database', 'DATABASE_NAME')