R
Railway•8mo ago
dalbanhi

Trouble connecting Django to PostgreSQL database

Hello, I am very much a new developer trying to deploy a Django app with PostgreSQL. After reading the docs, it seemed to me that I should do the following: * Provision a PostgreSQL database (which I have done, with Project ID: b90272c3-dbc8-4122-b259-9d4f79d6642d) * Connect it to my local Django project (which has been shown to work with my local PostgreSQL). * Eventually, I would then set these environment variables with a .env and it should just work after deploying. I'm sort of following this tutorial: https://kowe.io/guide/deploy-django-on-railway-postgresql However, I'm having trouble connecting my Railway provisioned database to my local Django project. When I try to run python3 manage.py migrate, the command just hangs for a very long time with no visible errors. I would really appreciate any help or direction.
Deploy Django on Railway - PostgreSQL - by clintonmatics - Kowe.
This tutorial is about deploying your Django web application to Railway, this time with a PostgreSQL database.
137 Replies
Percy
Percy•8mo ago
Project ID: b90272c3-dbc8-4122-b259-9d4f79d6642d
Brody
Brody•8mo ago
is your postgres database in the same project as your django service?
dalbanhi
dalbanhi•8mo ago
Hi Brody, thanks for answering. I think that's maybe what I'm trying to set up. I haven't hosted my Django project anywhere, it's just on my local machine. I am hoping to have it be hosted by Railway and use the Postgres DB, if that makes sense. I thought I would connect my local django project to the Railway Postgres DB first and then continue on. Does that make sense? Maybe I'm not expressing myself well of I have a misunderstanding in how this should work.
Brody
Brody•8mo ago
yeah it makes sense, you only have the database on railway right now show me the database thing in your settings.py please
dalbanhi
dalbanhi•8mo ago
Ah, I see...I think I can see how I can make a Django deployment that includes an already linked Postgres database now... In any case, this is my database setup in settings.py: DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql_psycopg2", "NAME": os.environ.get("PGDATABASE"), "USER": os.environ.get("PGUSER"), "PASSWORD": os.environ.get("PGPASSWORD"), "HOST": os.environ.get("PGHOST"), "PORT": os.environ.get("PORT"), } } And in my .env file, I have the actual values, copied over from the Variables section of the database Thank you so much for your help, by the way! I really appreciate it
Brody
Brody•8mo ago
lets try to do this in a way that doesnt involve putting the login to your database in a plaintext file in the same project as your postgres database, add an empty service
dalbanhi
dalbanhi•8mo ago
OK, thank you. I added it
Brody
Brody•8mo ago
screenshot please
dalbanhi
dalbanhi•8mo ago
No description
Brody
Brody•8mo ago
give it a better name haha
dalbanhi
dalbanhi•8mo ago
ok, I just went for the default Will this empty service host the environment files? Or would it be the django project? What makes sense as a name?
Brody
Brody•8mo ago
this empty service for now will hold the variables, and it will also eventually be where you deploy your app to so lets see a new screenshot after the name change
dalbanhi
dalbanhi•8mo ago
Ok, I see
dalbanhi
dalbanhi•8mo ago
Here you go
No description
Brody
Brody•8mo ago
for the sake of simplicity, just name it "bibblio" no need for complex names here
dalbanhi
dalbanhi•8mo ago
No description
dalbanhi
dalbanhi•8mo ago
Got it
Brody
Brody•8mo ago
open up the bibblio service variables and its raw editor, paste this in -
PGDATABASE=${{Postgres.PGDATABASE}}
PGUSER=${{Postgres.PGUSER}}
PGPASSWORD=${{Postgres.PGPASSWORD}}
PGHOST=${{Postgres.PGHOST}}
PGPORT=${{Postgres.PGPORT}}
PGDATABASE=${{Postgres.PGDATABASE}}
PGUSER=${{Postgres.PGUSER}}
PGPASSWORD=${{Postgres.PGPASSWORD}}
PGHOST=${{Postgres.PGHOST}}
PGPORT=${{Postgres.PGPORT}}
save and deploy
dalbanhi
dalbanhi•8mo ago
Got it, I was reading about shared variables, this is setting the variables from the Postgres DB to the local environment of what will be the django project, right?
dalbanhi
dalbanhi•8mo ago
It saved and deployed correctly
No description
Brody
Brody•8mo ago
click their eye icons, do they all show the variables correctly?
dalbanhi
dalbanhi•8mo ago
yup!
Brody
Brody•8mo ago
is there any other variables your app needs? if so, go ahead and add them there the end goal of this is so that you are able to delete your local .env file
dalbanhi
dalbanhi•8mo ago
I see. Yes, I have a few others, a Secret Key and a variable for whether the Django project is in Debug mode or not. I'll add them
Brody
Brody•8mo ago
ah you have debug set by a variable, okay you can keep the local .env file with only that debug variable set inside of it in the service variables youd want to have that debug variable set so your app runs in production mode when on railway
dalbanhi
dalbanhi•8mo ago
Ok, yes. So in my local env, I'll set DEBUG to True but when I add it to the service variable I set it to false.
Brody
Brody•8mo ago
correct so show me your local .env file now, it should only contain the debug line
dalbanhi
dalbanhi•8mo ago
OK, I added and deployed the changes and here's the .env file. Crucially, the DJANGO_DEBUG on the service is False
No description
No description
dalbanhi
dalbanhi•8mo ago
Ah sorry for the bad screenshot of the env file
dalbanhi
dalbanhi•8mo ago
No description
Brody
Brody•8mo ago
looks good to me do you have the railway cli installed?
dalbanhi
dalbanhi•8mo ago
Yes, I just installed it And I logged in and all
Brody
Brody•8mo ago
then in your project directory, run railway link and link to the bibblio service
dalbanhi
dalbanhi•8mo ago
done
No description
Brody
Brody•8mo ago
do you have migrations ready to apply?
dalbanhi
dalbanhi•8mo ago
Yes, I believe so
Brody
Brody•8mo ago
okay, run railway run python manage.py migrate this runs the given command locally, but with the linked service variables available, much better than keeping the database credentials in plaintext
dalbanhi
dalbanhi•8mo ago
Got it. That makes sense. It's running Or at least, I haven't gotten a completion nor error message
Brody
Brody•8mo ago
awsome
dalbanhi
dalbanhi•8mo ago
Thank you so much again, by the way. I've really been struggling with trying to deploy this app for months (on and off) and it's been frustrating. I really appreciate you helping me out
Brody
Brody•8mo ago
you sure waited a long time to ask for help haha
dalbanhi
dalbanhi•8mo ago
Haha, yea, I think my mistake was jumping from one deployment environment to another (Google Cloud, Vercel, Azure) and not finding ways to ask for helpthere
Brody
Brody•8mo ago
yeah i dont think any of those services have a good community
dalbanhi
dalbanhi•8mo ago
Yea, it was a lot of forum hunting and being frustrated, even trying to reach out to people who wrote tutorials on LinkedIn to no avail but anyway, I unfortunately have to run to a meeting for my day job soon (within 10 minutes) Do you think I might be able to ask for some help later? Or generally, what next steps do i take? Should I simply run
railway up
railway up
hopefully once the migrations run?
Brody
Brody•8mo ago
do you have your code on github?
dalbanhi
dalbanhi•8mo ago
GitHub
GitHub - dalbanhi/bibbl.io: A repository for Bibbl.io, a digital bo...
A repository for Bibbl.io, a digital book/journal organizer - dalbanhi/bibbl.io
Brody
Brody•8mo ago
Do you think I might be able to ask for some help later?
of course! then youd want to go the github deployment route and not railway up, we can get that hooked up when you are free again
dalbanhi
dalbanhi•8mo ago
Ok, sounds good! Thanks so much, Brody! Another quick question: why does the migration take so long? Is it simply because it's running this on the Railway server?
Brody
Brody•8mo ago
as mentioned, its not running this on the railway server, its ran locally
dalbanhi
dalbanhi•8mo ago
Ah ok. I think I'm understanding a bit better. However, I'm a little concerned as the migrations never ended/failed
Brody
Brody•8mo ago
screenshot please
dalbanhi
dalbanhi•8mo ago
So this is the terminal where I ran the railway command (first screenshot). However, the second screenshot is where I was running my project locally ( I forgot to kill it beforehand). I got the attached error. I'm thinking I should stop my local server and then re-try the railway command?
No description
No description
Brody
Brody•8mo ago
yes
dalbanhi
dalbanhi•8mo ago
Ok I'm running it again. Approximately how long do you usually take? The migrations for my project usually take very little time when I ran it locally before so I'm a little confused as to why it's taking significantly longer
Brody
Brody•8mo ago
nah theres for sure something wrong here what verison of the cli are you on
dalbanhi
dalbanhi•8mo ago
railwayapp 3.5.2
Brody
Brody•8mo ago
just for fun, what happens if you run python manage.py migrate
dalbanhi
dalbanhi•8mo ago
Ah. There, I get this error:
No description
Brody
Brody•8mo ago
okay thats normal what do you get if you run railway --help
dalbanhi
dalbanhi•8mo ago
it seems to be working well
No description
dalbanhi
dalbanhi•8mo ago
Hey Brody, again, I really appreciate your help. I had an idea since I'm not really sure how to proceed from here. Would it make sense to try to set up a django with postgres template and then try to add my github repo to the django service? Or would that possibly interfere with what we're trying to do here
Brody
Brody•8mo ago
nah something is going on here with your project and the cli, issue isnt with the railway side of things what does railway variables print well dont send that here, this is public
dalbanhi
dalbanhi•8mo ago
Oh, sorry--yea I should have realized
Brody
Brody•8mo ago
check the database on railway, does it have tables?
dalbanhi
dalbanhi•8mo ago
Yes it does
Brody
Brody•8mo ago
im kinda stumped right now
dalbanhi
dalbanhi•8mo ago
Hmm, ok, no worries Brody. I appreciate you trying to help me out anyway Do you think it would at all make sense to delete the services on my project and re-try it? I'm really not sure what else I could do
Brody
Brody•8mo ago
no, theres no issues with railway here
dalbanhi
dalbanhi•8mo ago
OK. Well, I think I will try to uninstall the CLI since you mentioned the issue seems to be there and re-install it Actually, one question. I should be connecting to the 'bibblio' service, right? not the "Postgres" service?
Brody
Brody•8mo ago
correct
dalbanhi
dalbanhi•8mo ago
Should I have a Procfile or anything in my root directory?
Brody
Brody•8mo ago
yes, but that has nothing to do with the cli
dalbanhi
dalbanhi•8mo ago
Hm ok, yea. Thanks for your help! I'll reach out again if some of my tinkering does anything
Brody
Brody•8mo ago
try railway shell and then run the migration command (without railway run)
dalbanhi
dalbanhi•8mo ago
When I run railway shell, it seems to immediately boot me off of it
No description
dalbanhi
dalbanhi•8mo ago
or like, I'm not able to type in a command before getting back to my regular command line
Brody
Brody•8mo ago
you are in the shell what terminal is this?
dalbanhi
dalbanhi•8mo ago
Its zsh
Brody
Brody•8mo ago
mac?
dalbanhi
dalbanhi•8mo ago
yea
Brody
Brody•8mo ago
okay so what happens if you run the migration command in the railway shell
dalbanhi
dalbanhi•8mo ago
I see... I didn't understand that I was in the shell. I thought it would be more like when you run a python repl or like when connecting to postgres with psql. I ran the command in the railway shell and it's still taking a long time, just like with the railway run option
Brody
Brody•8mo ago
something funky is happening can you temporarily remove your local .env file
dalbanhi
dalbanhi•8mo ago
ok I removed it and ran it again with railway shell . It seems to still be stuck
Brody
Brody•8mo ago
this is super odd okay desperate attempt, copy the raw variables from railway into a local .env file. then run the migration command without railway run or railway shell
dalbanhi
dalbanhi•8mo ago
ok So, wait, you mean to copy the actual values of the Postgres service, not the related variables like ${{Postgres.PGDATABASE}}, right?
Brody
Brody•8mo ago
yeah you'd need to replace the references with the values
dalbanhi
dalbanhi•8mo ago
Yea, it still didn't work. This is what my .env file looks like and the migrations are still hanging
No description
No description
Brody
Brody•8mo ago
you aren't in a railway shell right?
dalbanhi
dalbanhi•8mo ago
no, I don't think so-- when I ran railway shell it removed my virtual environment (venv), so I'm pretty sur e I'm in my regular shell
Brody
Brody•8mo ago
okay so then there is something wrong with your project or your network go download dbgate and use it to try to connect to the database
dalbanhi
dalbanhi•8mo ago
Hmm... ok. I have another meeting soon that I have to run to. So I'll try to do that and get back to you Brody, I really deeply do appreciate your help It's possible that its my network-- I'm a teacher so I work at a school and there might be some network restrictions
Brody
Brody•8mo ago
oh yes from personal experience, schools do have very restrictive firewalls
dalbanhi
dalbanhi•8mo ago
Ok. I'll try this back home and see if it works on my home network. If not, I'll try to work through DB Gate. I do have a bit of a busy schedule the next few days due to the holiday, but if I encounter any more issues, I'll be sure to ping you thanks so much again!
Brody
Brody•8mo ago
happy to help, and yep feel free to ping
dalbanhi
dalbanhi•8mo ago
Hey Brody! Guess what? Trying it out at home not on the school's firewall got me different (and fast) results! I tried to run both the migration command with/without railway run and in both cases, I got the same error (see attached). I ran into something similar when switching databases from local sqlite to postgres. What I did there is that I dumped my database into a json (it wasn't very large), then deleted the migrations, and then ran my migrations, and then finally reloaded my data using loaddata in django. I feel like that might work, but something in me thinks that there might be something obvious I am missing that would not involve me doing this, but somehow 'syncing' or merging my databases instead. What do you think, do you have any insights onto how to solve this new issue? This SO post (https://stackoverflow.com/questions/44651760/django-db-migrations-exceptions-inconsistentmigrationhistory) gave me some ideas last time but I'm wary of dropping my database since I know I wouldn't actually be able to do that with a team/larger database.
Stack Overflow
django.db.migrations.exceptions.InconsistentMigrationHistory
When I run python manage.py migrate on my Django project, I get the following error: Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.a...
No description
Brody
Brody•8mo ago
do you have any data in the database? like besides the tables existing?
dalbanhi
dalbanhi•8mo ago
I do have some data, like 'testing' data from when I was testing out my app. I wouldn't be sad if I had to drop those tables completely, but generally, I guess I've never really gone from dev to production and so I'm not really sure what the process for your data is supposed to go. If it were possible to keep that testing data, that would be nice but I'm not like wedded to it or anything. Also, Brody, thanks so much for your help. Do you have something like a Ko-Fi and/or like Venmo or something? You've really helped me out a bunch and honestly just made me feel like this was possible. I'd love to like buy you a cup of coffee in gratitude or something
Brody
Brody•8mo ago
I'm assuming your data in the database got messed up somehow, I don't know your project so I wouldn't really know on how to advice fixing it without wiping the database and re-running the migration again. i do have a bmac in my bio, but please don't feel any need to send me anything
dalbanhi
dalbanhi•8mo ago
got it, yea, I don't feel the need to, I just appreciate it! So, just as a follow up question, does just wiping my database mean deleting my migrations folder and trying to run teh migrations again?
Brody
Brody•8mo ago
wiping your database would mean you need to go into the volume's settings and wipe it, then running the migrations again with railway run
dalbanhi
dalbanhi•8mo ago
Hey Brody, thanks for getting back to me. I'm not sure what you exactly mean by 'wipe the volume's settings'. Would that be the Postgres database I just made on Railway? If I go into my Railway project, I have some tables there (see attached) but I don't actually have like any tables that correspond to any data that I might have created, like by creating users or anything. So I'm a little confused as to what/why I would wipe this volume
No description
Brody
Brody•8mo ago
so to recap you where having issues with the database and i recommended wiping it so that you can start with a fresh migration
dalbanhi
dalbanhi•8mo ago
Yes, exactly sorry, I should have mentioned that
Brody
Brody•8mo ago
so is that something you are going to do or are you going to try to resolve the errors manually?
dalbanhi
dalbanhi•8mo ago
I'm going to try to do that because I have a back up of my data/this is a testing project. I want to learn how to deploy this project and I'm not too fussed about the data
Brody
Brody•8mo ago
okay then go ahead and wipe the database's volume
dalbanhi
dalbanhi•8mo ago
So, what do you mean by that? Do I go into the Railway project and just delete all of these tables or is that something I need to do on my like local environment. I don't know if that makes sense
Brody
Brody•8mo ago
you simply press the wipe volume button in the databases's volume settings
dalbanhi
dalbanhi•8mo ago
OK. I'm having some trouble finding that but I will look for it. But afterwards, I should be able to run railway run python manage.py migrate and then the same with runserver and if I have no errors, I can push to my github repo, then connect my repo to the service and it should work?
Brody
Brody•8mo ago
click on the volume of the postgres service and yes you would want to run railway run python manage.py migrate and then you can redeploy the railway service
dalbanhi
dalbanhi•8mo ago
OK. I think I did both of those things. In order to redeploy the Railway service, you mean the Postgres database, right?
dalbanhi
dalbanhi•8mo ago
Hey Brody, so I'm able to create a new user with my Postgres database service from my local machine, which is exciting! However, I then tried to deploy by connecting to my repo and it said my deploy finished. In my deploy logs for that service, all I see is this (attached), which doesn't seem good. Do you have any tips as to what to explore? I'm not entirely sure why python would be a command that is not found on Railway's python
No description
Brody
Brody•8mo ago
please send your build logs
dalbanhi
dalbanhi•8mo ago
Ok. I think it's possible my issue is this: https://www.answeroverflow.com/m/1113611990238240778 (someone very knowledgeable giving some good tips there 😉 ), so I'm going to try what you are suggesting there. I too have a package.json too because I guess I tried to make a hybrid react/django app and I'm using webpack to bundle my react files into a index-bundle.js files. In the future, I'm probably sticking with more of a cleanly split react front end / django cors framework app, but I'm digressing. Below is my build log:
/bin/bash: line 1: python: command not found - Railway
I'm facing an issue while deploying my Django project on Railway. The deployment logs show the error message "/bin/bash: line 1: python: command not found," even though I have both a requirement.txt file and a runtime.txt file in my project. I'm unsure about the cause of this problem and would appreciate some guidance from a professional develop...
dalbanhi
dalbanhi•8mo ago
Pushing [==================================================>] 94.63MB 3bea1f1ca704 Pushing [==================================================>] 96.77MB 3bea1f1ca704 Pushing [==================================================>] 98.95MB 3bea1f1ca704 Pushing [==================================================>] 101.1MB 3bea1f1ca704 Pushing [==================================================>] 103.2MB 3bea1f1ca704 Pushing [==================================================>] 105.9MB 3bea1f1ca704 Pushing [==================================================>] 106.5MB 3bea1f1ca704 Pushed 3bea1f1ca704 750024d5-965f-44e2-9fd6-d06bda6f036d: digest: sha256:9efe2f346744dc0f2552ce1a4c817d49b2c63d92a3f9f902a8ede9444b948049 size: 2421 Publish time: 9.84 seconds (or at least the ending of it) Hmm, so I tried this railway.json file (the one you suggested) and my build failed this time
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"nixpacksPlan": {
"providers": ["python"],
"phases": {
"setup": {
"nixPkgs": ["...", "nodejs"]
}
}
},
"buildCommand": "npm ci"
},
"deploy": {
"startCommand": "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn webapp.wsgi",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS",
"nixpacksPlan": {
"providers": ["python"],
"phases": {
"setup": {
"nixPkgs": ["...", "nodejs"]
}
}
},
"buildCommand": "npm ci"
},
"deploy": {
"startCommand": "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn webapp.wsgi",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
It doesn't seem to have found a version of Django on my requirements.txt but I just did pip freeze > requirements.txt to save my package requirements (this is my build log):
6.026 ERROR: No matching distribution found for Django==5.0.3
6.251
6.251 [notice] A new release of pip is available: 23.0.1 -> 24.0
6.251 [notice] To update, run: pip install --upgrade pip
-----

Dockerfile:20
-------------------
18 | ENV NIXPACKS_PATH /opt/venv/bin:$NIXPACKS_PATH
19 | COPY . /app/.
20 | >>> RUN --mount=type=cache,id=s/bf770e00-35cd-438c-88ec-c9385e1bc086-/root/cache/pip,target=/root/.cache/pip python -m venv --copies /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt
21 |
22 | # build phase
-------------------
ERROR: failed to solve: process "/bin/bash -ol pipefail -c python -m venv --copies /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt" did not complete successfully: exit code: 1

Error: Docker build failed
6.026 ERROR: No matching distribution found for Django==5.0.3
6.251
6.251 [notice] A new release of pip is available: 23.0.1 -> 24.0
6.251 [notice] To update, run: pip install --upgrade pip
-----

Dockerfile:20
-------------------
18 | ENV NIXPACKS_PATH /opt/venv/bin:$NIXPACKS_PATH
19 | COPY . /app/.
20 | >>> RUN --mount=type=cache,id=s/bf770e00-35cd-438c-88ec-c9385e1bc086-/root/cache/pip,target=/root/.cache/pip python -m venv --copies /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt
21 |
22 | # build phase
-------------------
ERROR: failed to solve: process "/bin/bash -ol pipefail -c python -m venv --copies /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt" did not complete successfully: exit code: 1

Error: Docker build failed
Any help or insights at all you can point me to are super appreciated
Brody
Brody•8mo ago
full build logs please im pretty sure i have sent you this, but you havent been using it so ill send it again - https://bookmarklets.up.railway.app/log-downloader/
dalbanhi
dalbanhi•8mo ago
Ah, no I dont think I'd seen that. A useful tool
Brody
Brody•8mo ago
what version of python do you use locally?
dalbanhi
dalbanhi•8mo ago
Python 3.11.8
Brody
Brody•8mo ago
then you would want to add a runtime.txt to your repo with just 3.11 in it
dalbanhi
dalbanhi•8mo ago
hm ok. Gotcha, thanks for the tip -- I'll let you know how it goes!
dalbanhi
dalbanhi•8mo ago
So the deployment worked but I'm getting a TCP/IP connection error (log attached). I'm trying to debug the issue and any help would be appreciated!
dalbanhi
dalbanhi•8mo ago
omg, wow, I don't know how I would have caught that. Thanks for the second set of eyes
Brody
Brody•8mo ago
you arent the first to make that mistake
dalbanhi
dalbanhi•8mo ago
haha thanks I'll let you know how it goes, but I'm about to have dinner. Thank you sooo much again for all of your help It's truly amazing
Brody
Brody•8mo ago
happy to help
dalbanhi
dalbanhi•8mo ago
Thanks so much! After fixing some more typos, I'm getting no build nor deployment errors (I think... is it normal for a deploy log to look like this?):
Brody
Brody•8mo ago
yeah that looks good to me
dalbanhi
dalbanhi•8mo ago
Awesome! Yea, I think I'm not serving my static files for my django project correctly, but I think that's more of a django thing than a railway thing. I really appreciate all of the help, again!
Brody
Brody•8mo ago
yeah read up on the whitenoise docs
dalbanhi
dalbanhi•8mo ago
will do! Have a good night!
Brody
Brody•8mo ago
you too!
dalbanhi
dalbanhi•8mo ago
Hey Brody, I hope you're doing well. I had a quick question I was hoping you might be able to point me in the right direction of. I'm having some csrf troubles on my django app still (it's showing up now but I can't post anything). So, I'm trying to write to my own debugging logging file. I'm able to write to my own file on my local machine, but whenever I'm not sure where that logging file would exist on the Railway server. Would you happen to know anything about that? Any help/insight would be greatly appreciated
Brody
Brody•8mo ago
you would want to log to stdout/stderr not a file
Want results from more Discord servers?
Add your server