[Django]-ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value

28๐Ÿ‘

โœ…

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)}
๐Ÿ‘คMartijn Pieters

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',
    }
}

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

๐Ÿ‘คPhilip Mutua

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.

๐Ÿ‘คJulien Desaunay

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

Leave a comment