Search code examples
postgresqlelixirgraphqlasdf-vm

Connection refused when running mix ecto.setup command, packages installed with asdf


I'm trying to follow the GraphQL tutorial.

I'm used asdf to install the erlang, elixir and postgres. I'm using a macbook.

When I get to the step to run mix ecto.setup, I'm getting the following error:

20:44:47.318 [error] GenServer #PID<0.1605.0> terminating ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused (db_connection) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2 (connection) lib/connection.ex:622: Connection.enter_connect/5 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3 Last message: nil State: Postgrex.Protocol ** (Mix) The database for Community.Repo couldn't be created: an exception was raised: ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused (db_connection) lib/db_connection/connection.ex:163: DBConnection.Connection.connect/2 (connection) lib/connection.ex:622: Connection.enter_connect/5 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

What it could be?


Solution

  • You have to start postgres, according to the asdf-postgree docs you have to:

    pg_ctl start
    

    And in order to run the commands mix ecto.create and mix ecto.setup you have to create a user postgres with the password postgres, and the postgres user must have permissions to both LOGIN and CREATEDB, then you can:

    Create a default database:

    createdb default
    

    Log to that database, with:

    psql -d default
    

    And create the user with the permissions:

    # CREATE ROLE postgres LOGIN CREATEDB PASSWORD 'postgres';