a year ago
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](manage.py) migrate
stalls on connecting to the DB: do I need to export any env vars to make this connection work?
0 Replies
a year ago
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 -
python manage.py migrate &&
and setting a health check so that Railway knows when to hand over traffic.
Thanks very much.
Two follow ups:
How would I use the public host/port locally? Before posting this I exported them and ran the command again, same issue occurred.
How do I set a healthcheck? (Point me to docs if easier)
a year ago
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.simply add a
/health
endpoint that returns 200 when your app is ready to serve requests and set that in the service settings -
so, would you expect this to work:
export DATABASE_URL=postgresql... railway run python manage.py showmigrations
and on the start command, would that be:
python manage.py migrate && web: gunicorn ...
or web: python manage.py migrate && gunicorn...
a year ago
so, would you expect this to work:
export DATABASE_URL=postgresql…
railway run python manage.py showmigrations
i 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
web: python manage.py migrate && gunicorn...
I already have a health-check endpoint, looks like when being called railway isn't recognising success:
with connection.cursor() as cursor: cursor.execute("SELECT 1") return Response({"status": "healthy"}, status=status.HTTP_200_OK)
is it not ok if it has a body?
(this existing endpoint just tests if the BE and postgres are both awake/can connect)
a year ago
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?
a year ago
what error are you getting in the health check attempt logs?
I killed it after 14 builds and removed, deployed the migration change (which worked)
a year ago
you might need to remove any kind of auth or validation on that endpoint
a year ago
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?
a year ago
just the path, e.g /health
a year ago
no problem!
so my path is /health-check/
I can ping it on my machine:
$ curl -s -X GET '/health-check/' {"status":"healthy"}%
works fine, but on railway:
```====================
Starting Healthcheck
====================
Path: /health-check/
Retry window: 5m0s
Attempt #1 failed with status 400. Continuing to retry for 4m56s```
a year ago
this would mean you have some kind of validation or middleware on that path and its returning a 400 status code
My middleware (it’s a django project)
```
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.gzip.GZipMiddleware',
]```
a year ago
you would need to find a way to exclude your health check from any kind of middleware