How to fix this error – Elixir Phoenix: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused – :econnrefused

Standard

Has been rather a while since I last blogged.  And this update is rather technical so if you’re not into databases or Elixir programming, look away.

Largely documenting a solution to a rather cryptic error I encountered setting up a PostgreSQL database on Elixir Phoenix. Hope it helps someone!

If you get the rather strange error message below after running the mix ecto.create command:

** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused

** (Mix) The database for AppName.Repo couldn't be created: killed

Run this command if you have previously set up the Postgres app for MacOS before installing Postgres using Homebrew.  

/usr/local/opt/postgresql/bin/createuser -s postgres

The problem should be resolved!

The issue arises because the Postgres app creates a database superuser with your login name rather than the traditional role name of ‘postgres‘.  Mix’s ecto.create command can’t work as a consequence.

This post on Stack Overflow was helpful as was this blog post.