Help with connecting to DB
Hi,
I have a project with four services (python BE, redis, react FE, postgres).
In my python project directory on local machine I've run:
railway login
and railway link
, then linked to my backend service on railway.
now running railway run python manage.py migrate
stalls on connecting to the DB: do I need to export any env vars to make this connection work?24 Replies
Project ID:
c18400df-5dee-4fe7-bc56-a98db8d475bd
project ID: c18400df-5dee-4fe7-bc56-a98db8d475bd
By default Postgres's
DATABASE_URL
is using the private host and port that would only be usable within the context of the Railway project, and since railway run
runs the given command locally you would need to use the public database host and port to run migrations locally.
Alternately, you can have migrations run at start by changing your start command to -
and setting a health check so that Railway knows when to hand over traffic.Thanks very much.
Two follow ups:
1. How would I use the public host/port locally? Before posting this I exported them and ran the command again, same issue occurred.
2. How do I set a healthcheck? (Point me to docs if easier)
1. you could have a local .env file with
DATABASE_URL
being the database's public url so that when running locally you load from the .env and it replaces the private database url, im sure there are many other ways but thats what came to mind first.
2. simply add a /health
endpoint that returns 200 when your app is ready to serve requests and set that in the service settings - https://docs.railway.app/reference/healthchecksso, would you expect this to work:
and on the start command, would that be:
or
so, would you expect this to work: export DATABASE_URL=postgresql... railway run python manage.py showmigrationsi dont think it would as the variable injection done by
railway run
would overwrite the DATABASE_URL
variable you set before running the command, you would need to load variables from an .env file in code.
web: at the beginning
thank you
I already have a health-check endpoint, looks like when being called railway isn't recognising success:
is it not ok if it has a body?
(this existing endpoint just tests if the BE and postgres are both awake/can connect)
you already have whats called a readiness health check and thats even better as it also makes sure the database is active, and yep having a body is fine as long as its a GET type?
It is
what error are you getting in the health check attempt logs?
Attempt #1 failed with status 400. Continuing to retry for 4m59s
I killed it after 14 builds and removed, deployed the migration change (which worked)
you might need to remove any kind of auth or validation on that endpoint
There’s no auth
there is something returning 400
In the heath check config box, do you give it the endpoint name with or without the whole url prefix?
So do I give /heath-check, or the service-url/health-check
just the path, e.g
/health
Hmm
I’ll take a look, back in a bit, thank you so much for the help
no problem!
so my path is
works fine, but on railway:
/health-check/
I can ping it on my machine:
works fine, but on railway:
this would mean you have some kind of validation or middleware on that path and its returning a 400 status code
Will take a look, thank you
My middleware (it’s a django project)
I call it on my local machine with no auth/login etc
you would need to find a way to exclude your health check from any kind of middleware
Will take a look