DEV Community

Cover image for Django + PostgreSQL Deployment on Railway App

Django + PostgreSQL Deployment on Railway App

Meet Rajesh Gor on July 22, 2022

Introduction We have already seen the process of deployment of a Django app on Heroku, now we can move to another platform which is Rail...
Collapse
 
osahenru profile image
Osahenru

Beautiful piece, thank you so much but I'm getting this error message "Application Error
is your app correctly listening on $PORT?"

I have done every thing I can from creating an environment variables in my railway project to deleting my project severally but still after a successful deployment my project still crashes.

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

Have you entered the port number correctly from the postgres service on railway. I think that is probably the issue.

Collapse
 
osahenru profile image
Osahenru

Alright I'll try that out, although I didn't use postgresql for this project I used Django's default sql lite, I'll try this out and get back to you. thank you so much

Thread Thread
 
osahenru profile image
Osahenru

Fixed the issue. thanks for your assist

Thread Thread
 
surya7765 profile image
surya7765

Hey, May I know how you have fixed the issue?

Thread Thread
 
sgafgfjsa profile image
uwuwuw

@osahenru May I know how you fixed the issue?

Collapse
 
lbwaks profile image
Lucas Obwaku

Add logging to your app then git and deploy. On your dashboard under deployment there is a view logs button.Then click build logs .This will tell you the exact problem.

Collapse
 
osahenru profile image
Osahenru

Thank you I'll try this out too

Collapse
 
olaneat profile image
OlaNeat Ayoola

halo, i followed this and i'm getting this error when i apply migration
Image description

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

Thank you for reading and giving feedback,

Make sure you have created the PostgreSQL database service on Railway, specified the DATABASE_URL properly in the .env file, and setup dj-database-url for smooth working of the database operations.

Also you could try an alternative way instead of installing and setting up the dj-database-url:

Let's say my database url is given by railway as:

postgres://sjxgipufegmgsw:78cbb568e@ec2-52-4-104-184.compute-1.amazonaws.com:5432/dbmuget
Enter fullscreen mode Exit fullscreen mode

This is of the format,

postgresql://user:password@hostname:port/db_name
Enter fullscreen mode Exit fullscreen mode

So, we can convert this URL into a object like representation of the remote database in django settings.

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": "dbmuget",
        "USER": "sjxgipufegmgsw",
        "PASSWORD": "78cbb568e",
        "HOST": "ec2-52-4-104-184.compute-1.amazonaws.com",
        "PORT": 5432,
    }
}

Enter fullscreen mode Exit fullscreen mode

Try these few things out and let me if it still doesn't works.

Collapse
 
olaneat profile image
OlaNeat Ayoola

tnks this worked but now i'm getting a different err

Image description

Image description

Thread Thread
 
mr_destructive profile image
Meet Rajesh Gor

The address of the hostname must be taken from the URL and don't use Localhost.

Collapse
 
waylonwalker profile image
Waylon Walker

very thorough post! I need to give railway a try

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

Just curious. What kind of app will you deploy?

Collapse
 
waylonwalker profile image
Waylon Walker

No idea, I don't have anything that I need public compute for at the moment.

Collapse
 
jamesleonard profile image
James Leonard

please help me out with this error
Image description

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

There might be problems in the psycopg2 package installation, so I have already mentioned in the post:

pip install psycopg2

# If it does not work try
pip uninstall psycopg2
pip install psycopg2-binary
pip freeze >requirements.txt


# if still error persists try installing setuptools
pip install -U setuptools
pip install psycopg2
Enter fullscreen mode Exit fullscreen mode

If psycopg2 doesn't work, remove the packge from the requirements.txt file and add the psycopg2-binary package after installing.

Collapse
 
onyenzedon profile image
Onyenzedon

Hi Raj.. Please my own case is a bit different. I created different environments for the development and production. In development, I'm using sqlite3 database. In production, I'm using postgresql though through dj_database_url. Please, how do I createsuper user in production environment?

Collapse
 
lbwaks profile image
Lucas Obwaku

After a successful deployment of a Django app to railways how do I add a superuser admin?

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

Make sure your app is running on railway, and you can also add the railway database locally and run python manage.py createsuperuser. From this you can connect the railway db locally and create a superuser. From there on you can use admin from the deployed app.

Collapse
 
lbwaks profile image
Lucas Obwaku

You should add how to deal with media files in production. This will make this blog complete

Thread Thread
 
daveparr profile image
Dave Parr

This helped me today, thanks for the updates.

Collapse
 
giftedstan profile image
giftedstan

Hello, this is a beautiful write-up, i followed it and i have my app deployed, but i have one request, on heroku, we have an app called heroku scheduler, whats the alternative on railway?

Collapse
 
mohammadanarul profile image
mohammadanarul

Railways have developed complete guidelines. Thank you.

Collapse
 
jamesleonard profile image
James Leonard

Image description

Collapse
 
tejasnirmal123 profile image
Tejas Nirmal

Uploading image

I have this error while deploying on Railway, what should I do

Collapse
 
ndurepablo profile image
Pablo Ndure

Hello! How should I configure Django to serve media files?

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

I haven't tried media files, though you can look into this post, it might be similar to it.

Collapse
 
tomjames156 profile image
Akinwande Tomisin

Image description

Please I'm getting this error, any idea how to fix it.