Skip to main content

Dealing with the PostgreSQL connection refused error in the Phoenix framework

·2 mins

I wanted to learn to use the Phoenix web development framework. I was going through the Up and Running guide on the Phoenix framework’s site. When I ran the mix ecto.create command to create the database, I kept getting the following error:

15:45:35.314 [error] Postgrex.Protocol (#PID<0.344.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused – :econnrefused
** (Mix) The database for Hello.Repo couldn’t be created: killed

I noticed the following instruction in the tutorial:

Then configure your database in config/dev.exs

Without providing any more details. After some trial and error, I got the tutorial project up and running. I learned two requirements to get the mix ecto.create command to work.

  • You must start the PostgreSQL server.
  • Make sure the database user and password are correct.

Starting the Database Server #

As someone inexperienced with web development, I didn’t realize you have to start a database server to create a database. I used the Postgres.app Mac app to create and start the database server.

Configuring the Database User and Password #

The Phoenix tutorial assumes the database user and password are both postgres. If your postgres user has a different password, you must either change the password when running PostgreSQL or create a new user and password and use them in Phoenix.

Inside the project folder is a config folder. Inside that folder is a file called dev.exs. Open this file in a text editor. At the top of the file, you should see text similar to the following:

# Configure your database
config :hello, Hello.Repo,
  username: "postgres",
  password: "postgres",
  hostname: "localhost",
  database: "hello_dev",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

If your postgres user has a different password, enter it in the password field. If you create a new user, enter the name in the username field and the password in the password field.