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

staudtdigitalHOBBY

9 months 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 ENABLEALPLINEPRIVATE_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:

DATABASEPUBLICURL="${{Postgres.DATABASEPUBLICURL}}"
DATABASEURL="${{Postgres.DATABASEURL}}"
PGDATABASE="${{Postgres.PGDATABASE}}"
PGHOST="${{Postgres.PGHOST}}"
PGPASSWORD="${{Postgres.PGPASSWORD}}"
PGPORT="${{Postgres.PGPORT}}"
PGUSER="${{Postgres.PGUSER}}"
ENABLEALPINEPRIVATE_NETWORKING="true"

And here are the database variables in railway Postgres:

DATABASEPUBLICURL="postgresql://${{PGUSER}}:${{POSTGRESPASSWORD}}@${{RAILWAYTCPPROXYDOMAIN}}:${{RAILWAYTCPPROXYPORT}}/${{PGDATABASE}}" DATABASEURL="postgresql://${{PGUSER}}:${{POSTGRESPASSWORD}}@${{RAILWAYPRIVATEDOMAIN}}:5432/${{PGDATABASE}}" PGDATA="/var/lib/postgresql/data/pgdata" PGDATABASE="${{POSTGRESDB}}"
PGHOST="${{RAILWAYPRIVATEDOMAIN}}"
PGPASSWORD="${{POSTGRESPASSWORD}}" PGPORT="5432" PGUSER="${{POSTGRESUSER}}"
POSTGRES_DB="railway"

Thank you.

View Deploy details

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

Solved

1 Replies

staudtdigitalHOBBY

9 months 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 9 months ago