I’ve created two Phoenix apps.
With both of them, I was momentarily stumped by a weird error message.
Be warned!
The Problem
After creating a new Phoenix app, when we try to do something, e.g. mix ecto.create we see an error like this.
If you look very closely, there is a pretty good hint at the problem: %Postgrex.Error{message: "tcp connect: nxdomain", postgres: nil}.
There appears to be some problem connecting to the database.
The Solution
I found a single Github comment which solved my problem.
I had the same nxdomain problem.
It turned out that my issue was the the env var PGHOST=/tmp, which is the first url that postgrex is using to connect to the db.
I got around this by setting hostname: “localhost” in my config.exs.
So there we have it.
We must specify a hostname: "localhost" option for our database configuration.
This tells Ecto to connect at “localhost” rather than trying to follow $PGHOST.
I suspect this has something to do with my environment.
I connect to Postgres.app via Unix sockets stored at this location.
Here’s a post with a little more information about that config.
Finally, here’s an example commit in an app I made fixing the problem.