skip to Main Content

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


  1. Use this setting for postgresql:

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

    }

    REF: https://docs.djangoproject.com/en/4.1/ref/settings/#databases

    Login or Signup to reply.
  2. 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),
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search