how to fix raise ImproperlyConfigured("settings.DATABASES is improperly configured. when deploying on railway

Viewed 51

hey guys so i'm follwing this guide https://dev.to/mr_destructive/django-postgresql-deployment-on-railway-app-d54 on how to deploy my django project on railway i have everything set locally, it working but once i deploy, the app crashes returning this err

File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/home/olaneat/Desktop/files/project/django/job/lib/python3.8/site-packages/django/db/backends/dummy/base.py", line 20, 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.

below is my database setting


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'HOST': os.environ.get('PGHOST'),
        'NAME': os.environ.get('PGDATABASE'),  
        'USERNAME': os.environ.get('PGUSER'),
        'PASSWORD': os.environ.get('PGPASSWORD'),
        'PORT':os.environ.get('PGPORT')
    }
}


by the way, this is working on my localhost, i only get this err when i deploy to railway can someone pls help out

2 Answers

You need to add the relevant environment variable names to the railway app variable settings. To add variables click + New Variable and it will prompt you to enter the variable name and its value in it. Copy the Database Connection URL from the PostgreSQL service settings and paste in the app variables DATABASE_URL's value

enter image description here

enter image description here

If you think the method isn't working for you, you can also try the second approach. You can add the database URL as a single string as well, this is also an alternative rather than specifying all the separate fields.

# pip install dj_database_url python-dotenv

import dj_database_url
import os
from dotenv import load_dotenv

load_dotenv(os.path.join(BASE_DIR, '.env'))

DATABASE_URL = os.getenv("DATABASE_URL")

DATABASES = {
    "default": dj_database_url.config(default=DATABASE_URL, conn_max_age=1800),
}
Related