ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value
Asked Answered
T

5

24

I'm in the midst of setting up my Django project on Heroku. I have been following the documentation, but when I foreman start I receive an error that I can't quite figure out. I have set up my engine files, but it doesn't seem to want to work.

Full traceback:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/base.py", line 415, in handle
    return self.handle_noargs(**options)
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/Users/nir/nirla/venv/lib/python2.7/site-packages/django/db/backends/__init__.py", line 160, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Users/nir/nirla/venv/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.

Someone suggested using ./manage.py diffsettings and showing the DATABASES part:

DATABASES = {'default': {'AUTOCOMMIT': True, 'ENGINE': 'django.db.backends.dummy', 'ATOMIC_REQUESTS': False, 'NAME': '', 'TEST_MIRROR': None, 'CONN_MAX_AGE': 0, 'TEST_NAME': None, 'TIME_ZONE': 'UTC', 'TEST_COLLATION': None, 'PORT': '', 'HOST': '', 'USER': '', 'TEST_CHARSET': None, 'PASSWORD': '', 'OPTIONS': {}}}

I can't seem to figure out what it means, but it doesn't look right on the surface.

Here is part of my settings.py that I think could be relevant to this problem:

import os
import dj_database_url

ON_HEROKU = os.environ.get('ON_HEROKU')
HEROKU_SERVER = os.environ.get('HEROKU_SERVER')

if ON_HEROKU:
    
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'postgresql',
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
            'USER': '',
            'PASSWORD': '',
            'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
            'PORT': '',  
        }
    }


DATABASES['default'] =  dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATIC_URL = '/static/'

# only refers to the location where your static files should end up after running manage.py collectstatic. you shouldn't really need collectstatic) when developing locally
STATIC_ROOT = 'staticfiles'


STATICFILES_DIRS = (    
    os.path.join(BASE_DIR, '../static'),
)

Tiannatiara answered 27/9, 2014 at 23:57 Comment(0)
R
29

You are using the dj-database-url module to set DATABASES['default']. Whatever comes before the line:

DATABASES['default'] =  dj_database_url.config()

is meaningless as you replace your database configuration in its entirety. The dj_database_url.config() loads your database configuration from the DATABASE_URL environment variable, or returns {} if the variable is not set.

Judging by your error, you didn't set the DATABASE_URL at all. Judging by the code preceding the dj_database_url.config() line, you should not be using the dj_database_url.config() function at all.

If you did want to use it, at least build a default URL:

if ON_HEROKU:
    DATABASE_URL = 'postgresql://<postgresql>'
else:
    DATABASE_URL = 'sqlite:///' + os.path.join(BASE_DIR, 'db.sqlite3')

DATABASES = {'default': dj_database_url.config(default=DATABASE_URL)}
Riven answered 28/9, 2014 at 0:12 Comment(0)
B
8

You can use following setting for localhost

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'DatabaseName',
        'USER': 'DatabaseUserName',
        'PASSWORD': 'DatabaseUserpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}
Bahamas answered 27/4, 2016 at 13:11 Comment(1)
But can you use it for production?Brahmana
P
3

Encountered this issue when running my github workflows with github actions. So I found the following work around:

POSTGRES_DB = os.environ.get("POSTGRES_DB") #database name
POSTGRES_PASSWORD = os.environ.get("POSTGRES_PASSWORD") # database user password
POSTGRES_USER = os.environ.get("POSTGRES_USER") # database username
POSTGRES_HOST = os.environ.get("POSTGRES_HOST") # database host
POSTGRES_PORT = os.environ.get("POSTGRES_PORT") # database port


POSTGRES_READY = (
    POSTGRES_DB is not None
    and POSTGRES_PASSWORD is not None
    and POSTGRES_USER is not None
    and POSTGRES_HOST is not None
    and POSTGRES_PORT is not None
)

print(POSTGRES_READY)

if POSTGRES_READY:
    DATABASES = {
        "default": {
            "ENGINE": "django.db.backends.postgresql",
            "NAME": POSTGRES_DB,
            "USER": POSTGRES_USER,
            "PASSWORD": POSTGRES_PASSWORD,
            "HOST": POSTGRES_HOST,
            "PORT": POSTGRES_PORT,
        }
    }

Then your .env file:

export DEBUG=True
export DJANGO_SECRET_KEY=CI_CD_TEST_KEY
export POSTGRES_USER=taxi
export POSTGRES_PASSWORD=taxi
export POSTGRES_DB=pdm
export POSTGRES_PORT=5432
export POSTGRES_HOST=localhost

Then run source .env

Pompea answered 19/1, 2022 at 9:0 Comment(0)
C
2

I had the same issue using django-tenant-users when running the command:

python manage.py setup_dtu_tenants

It was because I had a folder named "settings" inside my project folder at the same level as my settings.py. After removing this folder, the issue is gone.

Carrageen answered 13/11, 2020 at 14:20 Comment(2)
Your answer does not seem to match the error message that appears in the code shared by @ApathyBear. Please, do read the question again.Trilbie
This does not seem to answer this question, but it helped my with my problem. The problem was in having myprojectname/settings/ folder for my settings and apps/settings appGebhart
W
0

With "Django==3.1.7", I got the same error when using the code below in "settings.py":

# "settings.py"

from django.db import connection 

print(connection.queries) # Causes error

So, I removed "print(connection.queries)" as shown below, then the error was solved:

# "settings.py"

from django.db import connection 

# print(connection.queries) # Causes error
Wove answered 17/8, 2022 at 17:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.