Connecting PostgreSql database to django project in github

Hello everyone, I want to connect railway postgresql database to my django project, because when I update my project in github all data is deleted. So I tried to update the database by following the steps below. 1- I changed settings.py file in accordance with railway template. (picture ending with 848) I didn't remove " " 's. Can it be a problem? I copied the information in the postgresql database variables. 2-I added 5 service variables in my service. The pictures ending with 052 and 158. After doing these two steps my application has failed. What should I do, can you help me ?
No description
No description
No description
51 Replies
Percy
Percy10mo ago
Project ID: 81da0120-0b23-44c7-8b07-ddf25f6e0008
aydin26532
aydin26532OP10mo ago
this is my project id = 81da0120-0b23-44c7-8b07-ddf25f6e0008
Brody
Brody10mo ago
can you please send full screenshots, your cropped screenshots are cutting off info
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
I choose from dropdown
SlowSloth
SlowSloth10mo ago
in the deploy log, do you get ValueError?
Brody
Brody10mo ago
this is django??
SlowSloth
SlowSloth10mo ago
seems so
aydin26532
aydin26532OP10mo ago
File "/opt/venv/lib/python3.11/site-packages/django/conf/init.py", line 190, in init mod = importlib.import_module(self.SETTINGS_MODULE) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/root/.nix-profile/lib/python3.11/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap>", line 1204, in _gcd_import File "<frozen importlib._bootstrap>", line 1176, in _find_and_load File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 690, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 940, in exec_module File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed File "/app/yv_optimizer/settings.py", line 98, in <module> 'NAME': os.environ["railway"], ~~^^^^^^^^^^^ File "<frozen os>", line 679, in getitem KeyError: 'railway' [2024-01-29 18:27:05 +0000] [10] [INFO] Worker exiting (pid: 10) [2024-01-29 18:27:05 +0000] [7] [INFO] Shutting down: Master [2024-01-29 18:27:05 +0000] [7] [INFO] Reason: Worker failed to boot. this is last lines of the deoplyment
SlowSloth
SlowSloth10mo ago
os.environ["railway"] will raise an error is there is no such env var
aydin26532
aydin26532OP10mo ago
yes django
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
but pgdatabase name is railway
SlowSloth
SlowSloth10mo ago
"NAME": os.environ["PGDATABASE"]
"NAME": os.environ["PGDATABASE"]
will solve it
aydin26532
aydin26532OP10mo ago
I wrote as "NAME": os.environ["railway"] should I write as "NAME": os.environ[ralway] on github/settings.py
SlowSloth
SlowSloth10mo ago
"NAME": os.environ["PGDATABASE"]
aydin26532
aydin26532OP10mo ago
ok then I amchanging this from railway to PGATABASE in github /settings.py
SlowSloth
SlowSloth10mo ago
think of environment variable as a dictionary
{
"PGDATABSE": "railway"
}
{
"PGDATABSE": "railway"
}
Django's learning curve's quite high, once you get the hang of it it's full CRUD APIs in minutes. Goodluck
aydin26532
aydin26532OP10mo ago
"NAME": os.environ[{ "PGDATABSE": "railway" }] is it the ne syntax
SlowSloth
SlowSloth10mo ago
"NAME": os.environ["PGDATABASE"],
"NAME": os.environ["PGDATABASE"],
replace entire line 98 with this
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
ok this is updated
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
this is the new error
Brody
Brody10mo ago
put the USER back to what you had before
aydin26532
aydin26532OP10mo ago
No description
Brody
Brody10mo ago
yes
aydin26532
aydin26532OP10mo ago
do you mean I should only copy everything in here, because I add password etc from railway and change to PGPASSWORD with passwordd variable in railway
Brody
Brody10mo ago
what the template has is correct
aydin26532
aydin26532OP10mo ago
ok, as I understand for now there is no error for datbase but how can I see my tables
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
No description
Brody
Brody10mo ago
have you ran migrations?
aydin26532
aydin26532OP10mo ago
no, should I migrate them from github sorry I am new, if it is simple question
Brody
Brody10mo ago
what's your current start command and how is it set?
aydin26532
aydin26532OP10mo ago
actually I don't understand but when I start my service on local, I wrote firstly, env/Scripts/activate then python manage.py runserver
Brody
Brody10mo ago
what's your current start command used by railway and how is it set?
aydin26532
aydin26532OP10mo ago
as I understand you are talking about CLI, but I didn't use CLI. I only uploaded github then, import from github. si there any ocument to see this
Brody
Brody10mo ago
sorry that's not what I'm talking about please send the build table at the top of the build logs
aydin26532
aydin26532OP10mo ago
No description
aydin26532
aydin26532OP10mo ago
is it what you said
SlowSloth
SlowSloth10mo ago
You need to migrate your railway database first, as there are no tables in there. To do that you need to run python manage.py migrate if you changed your models you need to make migration files first with python manage.py makemigrations it seems that you don't understand these concepts yet so I suggest you watch this first https://www.youtube.com/watch?v=F5mRW0jo-U4
freeCodeCamp.org
YouTube
Python Django Web Framework - Full Course for Beginners
Learn the Python Django framework with this free full course. Django is an extremely popular and fully featured server-side web framework, written in Python. Django allows you to quickly create web apps. 💻Code: https://github.com/codingforentrepreneurs/Try-Django ⭐️Course Contents ⭐️ ⌨️ (0:00:00) 1 - Welcome ⌨️ (0:01:14) 2 - Installing to Get ...
SlowSloth
SlowSloth10mo ago
There are many ways to do it in your case the easiest would be using railway CLI
cd <to where your manage.py is>
cd <to where your manage.py is>
railway run python manage.py migrate
railway run python manage.py migrate
railway cli will inject correct env vars and migrate your cloud environment
aydin26532
aydin26532OP10mo ago
actually I use these commands and using django project on my localhost, but I don't know where to write python manage.py migrate/python manage.py makemigrations
SlowSloth
SlowSloth10mo ago
you can write a bash script and run that you cannot exec into railway pods ie. you cannot access railway terminal
aydin26532
aydin26532OP10mo ago
ok , then firstly I have to learn about railway CLI right?
SlowSloth
SlowSloth10mo ago
yes
SlowSloth
SlowSloth10mo ago
you can also use custom start command https://docs.railway.app/guides/deployments#start-command
Railway Docs
Deployments | Railway Docs
Documentation for Railway
SlowSloth
SlowSloth10mo ago
Or you can write a dockerfile, there are quite few ways to do it but in your case, railway CLI
aydin26532
aydin26532OP10mo ago
ok thank you very much for your help
Want results from more Discord servers?
Add your server