R
Railway4mo ago
tjh

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
Percy
Percy4mo ago
Project ID: c18400df-5dee-4fe7-bc56-a98db8d475bd
tjh
tjhOP4mo ago
project ID: c18400df-5dee-4fe7-bc56-a98db8d475bd
Brody
Brody4mo 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 && <your current start command>
python manage.py migrate && <your current start command>
and setting a health check so that Railway knows when to hand over traffic.
tjh
tjhOP4mo ago
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)
Brody
Brody4mo ago
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/healthchecks
tjh
tjhOP4mo ago
so, would you expect this to work:
export DATABASE_URL=postgresql...
railway run python manage.py showmigrations
export DATABASE_URL=postgresql...
railway run python manage.py showmigrations
and on the start command, would that be:
python manage.py migrate && web: gunicorn ...
python manage.py migrate && web: gunicorn ...
or
web: python manage.py migrate && gunicorn...
web: python manage.py migrate && gunicorn...
Brody
Brody4mo 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...
web: python manage.py migrate && gunicorn...
tjh
tjhOP4mo ago
thank you 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)
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)
Brody
Brody4mo 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?
tjh
tjhOP4mo ago
It is
Brody
Brody4mo ago
what error are you getting in the health check attempt logs?
tjh
tjhOP4mo ago
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)
Brody
Brody4mo ago
you might need to remove any kind of auth or validation on that endpoint
tjh
tjhOP4mo ago
There’s no auth
Brody
Brody4mo ago
there is something returning 400
tjh
tjhOP4mo ago
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
Brody
Brody4mo ago
just the path, e.g /health
tjh
tjhOP4mo ago
Hmm I’ll take a look, back in a bit, thank you so much for the help
Brody
Brody4mo ago
no problem!
tjh
tjhOP4mo ago
so my path is /health-check/ I can ping it on my machine:
$ curl -s -X GET '<URL>/health-check/'
{"status":"healthy"}%
$ curl -s -X GET '<URL>/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
====================

Starting Healthcheck

====================


Path: /health-check/

Retry window: 5m0s


Attempt #1 failed with status 400. Continuing to retry for 4m56s
Brody
Brody4mo ago
this would mean you have some kind of validation or middleware on that path and its returning a 400 status code
tjh
tjhOP4mo ago
Will take a look, thank you 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',
]
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',
]
I call it on my local machine with no auth/login etc
Brody
Brody4mo ago
you would need to find a way to exclude your health check from any kind of middleware
tjh
tjhOP4mo ago
Will take a look
Want results from more Discord servers?
Add your server