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 ?
51 Replies
Project ID:
81da0120-0b23-44c7-8b07-ddf25f6e0008
this is my project id = 81da0120-0b23-44c7-8b07-ddf25f6e0008
can you please send full screenshots, your cropped screenshots are cutting off info
I choose from dropdown
in the deploy log, do you get ValueError?
this is django??
seems so
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
os.environ["railway"] will raise an error is there is no such env var
yes django
but pgdatabase name is railway
will solve it
I wrote as "NAME": os.environ["railway"]
should I write as "NAME": os.environ[ralway] on github/settings.py
"NAME": os.environ["PGDATABASE"]
ok then I amchanging this from railway to PGATABASE in github /settings.py
think of environment variable as a dictionary
Django's learning curve's quite high, once you get the hang of it it's full CRUD APIs in minutes. Goodluck
"NAME": os.environ[{
"PGDATABSE": "railway"
}] is it the ne syntax
replace entire line 98 with this
ok this is updated
this is the new error
put the USER back to what you had before
yes
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
what the template has is correct
ok, as I understand for now there is no error for datbase but how can I see my tables
have you ran migrations?
no, should I migrate them from github
sorry I am new, if it is simple question
what's your current start command and how is it set?
actually I don't understand but when I start my service on local, I wrote firstly, env/Scripts/activate
then python manage.py runserver
what's your current start command used by railway and how is it set?
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
sorry that's not what I'm talking about
please send the build table at the top of the build logs
is it what you said
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-U4freeCodeCamp.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 ...
There are many ways to do it in your case the easiest would be using railway CLI
railway cli will inject correct env vars and migrate your cloud environment
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
you can write a bash script and run that
you cannot exec into railway pods
ie. you cannot access railway terminal
ok , then firstly I have to learn about railway CLI
right?
yes
you can also use custom start command
https://docs.railway.app/guides/deployments#start-command
Or you can write a dockerfile, there are quite few ways to do it but in your case, railway CLI
ok thank you very much for your help