Help with connecting to DB

tony-hunter
HOBBY

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

tony-hunter
HOBBY

a year ago

project ID: c18400df-5dee-4fe7-bc56-a98db8d475bd


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.


tony-hunter
HOBBY

a year 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)


a year 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 -


tony-hunter
HOBBY

a year ago

so, would you expect this to work:

export DATABASE_URL=postgresql... railway run python manage.py showmigrations


tony-hunter
HOBBY

a year ago

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...

tony-hunter
HOBBY

a year ago

thank you


tony-hunter
HOBBY

a year ago

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?


tony-hunter
HOBBY

a year ago

(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?


tony-hunter
HOBBY

a year ago

It is


a year ago

what error are you getting in the health check attempt logs?


tony-hunter
HOBBY

a year ago

Attempt #1 failed with status 400. Continuing to retry for 4m59s


tony-hunter
HOBBY

a year ago

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


tony-hunter
HOBBY

a year ago

There’s no auth


a year ago

there is something returning 400


tony-hunter
HOBBY

a year ago

In the heath check config box, do you give it the endpoint name with or without the whole url prefix?


tony-hunter
HOBBY

a year ago

So do I give /heath-check, or the service-url/health-check


a year ago

just the path, e.g /health


tony-hunter
HOBBY

a year ago

Hmm


tony-hunter
HOBBY

a year ago

I’ll take a look, back in a bit, thank you so much for the help


a year ago

no problem!


tony-hunter
HOBBY

a year ago

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


tony-hunter
HOBBY

a year ago

Will take a look, thank you


tony-hunter
HOBBY

a year ago

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',
]```


tony-hunter
HOBBY

a year ago

I call it on my local machine with no auth/login etc


a year ago

you would need to find a way to exclude your health check from any kind of middleware


tony-hunter
HOBBY

a year ago

Will take a look


Help with connecting to DB - Railway Help Station