Django Project: django.db.utils.OperationalError: could not translate host name "postgres.railway.internal" to address: Name or service not known
staudtdigital
HOBBYOP

2 years ago

Hi. I'm deploying a Django project and I'm trying to connect it to a Postgres database within Railway. Both my service (the one with the Django project) and my Postgres database are in the same Railway project.

When I deploy it, this is the error message I get:

ERROR: failed to solve: process "/bin/bash -ol pipefail -c sleep 3 && python manage.py migrate" did not complete successfully: exit code: 1

When I scroll up to see what caused the problem, this is what is says:

django.db.utils.OperationalError: could not translate host name "postgres.railway.internal" to address: Name or service not known

Some other people have had this problem, and I think it's usually because their database and Django service aren't both in the Railway project. Mine are.

This started happening after I added "python manage.py migrate" to the "release:" section of my Procfile. Before I did this, everything deployed fine, but when I tried to create a new user in the app, it said it couldn't create a user because the "auth_user" table didn't exist. This makes sense, because the Postgres database in Railway didn't have any tables. I figured that by running "python manage.py migrate" I could migrate my tables to the Postgres database---but instead I got the error message that "postgres.railway.internal" was an unknown service.

Somebody else seemed to have a similar problem, and it was suggested that they add "sleep 3" to the start command and ENABLE_ALPLINE_PRIVATE_NETWORKING=true to the service variables. I did this, and it didn't seem to change anything.

My GitHub repo is private, but here's my Procfile:

release: sleep 3 && python manage.py migrate

web: gunicorn 'myappname.wsgi'

And here's the relevant section of my settings.py:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.postgresql_psycopg2',

'NAME': (os.getenv('PGDATABASE')),

'USER': (os.getenv('PGUSER')),

'PASSWORD': (os.getenv('PGPASSWORD')),

'HOST': os.getenv('PGHOST'),

'PORT': os.getenv('PGPORT'),

}

And here are my relevant service variables:

DATABASE_PUBLIC_URL="${{Postgres.DATABASE_PUBLIC_URL}}"

DATABASE_URL="${{Postgres.DATABASE_URL}}"

PGDATABASE="${{Postgres.PGDATABASE}}"

PGHOST="${{Postgres.PGHOST}}"

PGPASSWORD="${{Postgres.PGPASSWORD}}"

PGPORT="${{Postgres.PGPORT}}"

PGUSER="${{Postgres.PGUSER}}"

ENABLE_ALPINE_PRIVATE_NETWORKING="true"

And here are the database variables in railway Postgres:

DATABASE_PUBLIC_URL="postgresql://${{PGUSER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{PGDATABASE}}"

DATABASE_URL="postgresql://${{PGUSER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:5432/${{PGDATABASE}}"

PGDATA="/var/lib/postgresql/data/pgdata"

PGDATABASE="${{POSTGRES_DB}}"

PGHOST="${{RAILWAY_PRIVATE_DOMAIN}}"

PGPASSWORD="${{POSTGRES_PASSWORD}}"

PGPORT="5432"

PGUSER="${{POSTGRES_USER}}"

POSTGRES_DB="railway"

Thank you.

View Deploy details

ⓘ Deployment information is only viewable by project members and Railway employees.

Solved

1 Replies

staudtdigital
HOBBYOP

2 years ago

Hey, I just solved this problem. To anyone else who's dealing with something similar, this is what my problem was:

In my Procfile, I had:

release: python manage.py migrate

web: gunicorn myappname.wsgi

I delete the "release:" line and changed the "web:" line to

web: python manage.py migrate && gunicorn myappname.wsgi

And everything worked great.


Status changed to Solved brody almost 2 years ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...