psql: error: could not translate host name "db" to address: nodename nor servname provided, or not known

Hello. So I have a django app, it was deployed without db and worked fine. I’ve added to dockerfile a new migration step RUN python migrate. Added also DATABASE_URL to web app.
In I have -

    "default": dj_database_url.config(default=os.environ.get('DATABASE_URL'))

I’ve tested locally this new update and it worked fine. But when I run fly deploy I get django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known.
Tried different options, f.e. psql -h ***.flycast -p 5432 -U postgres -W ******** and it returns - psql: error: could not translate host name "***.flycast" to address: nodename nor servname provided, or not known
In app console, I’m getting same error when running directly python migrate. Restarting db app didn’t work.

Hi… Try:

fly dig db-app-name.flycast

…where db-app-name is the name of the database app (and not your Django web app). You can use fly apps list to reveal the list of choices.

I’m getting this:

;; opcode: QUERY, status: NOERROR, id: 34207
;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;***-production.flycast.	IN	 AAAA

***-production.flycast.	5	IN	AAAA	fdaa:3:f097:0:1::6

This looks ok?

That does look ok.

How about the following?

$ fly ssh console -a ***-production
# su postgres
% psql -h ***-production.flycast

I’m able to login to db with those commands:

postgres@784e439a223258:/$ psql -h ***.flycast
Password for user postgres:
psql (15.3 (Debian 15.3-1.pgdg120+1))
Type "help" for help.

postgres=# \q

I guess that means, that app logic incorrectly interprets database url.
But then, why I can’t do it with just psql, without logging in to db machine. Does it mean, that publicly that host is available only within environment or I need to modify fly configs to expose postgres db port?

Right… By default, the .flycast and .internal addresses are only accessible to your Fly Machines (excluding the builder).

There are three different ways to allow connections from your local computer, but you needn’t to do so at the moment—if I understand your situation correctly.

If you instead remove the RUN python migrate from your Dockerfile and put it in a release_command, then things should work out as you intended.

(The builder doesn’t have access to the database—as alluded to above. This confuses a lot of people…)

Thanks, your variant worked.
I also fully re-created postgres machine and re-attached it to web app with 2 commands, which is cool.

fly postgres create
fly postgres attach db-production -a my-web-app
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.