Unable to user postgrex in a mix task: "unknown registry: Postgrex.TypeManager"

Hi!

I have a project that is only a collection of mix tasks, there is no ecto, phoenix, supervisor, nothing.
I tried to setup the postgrex library but I’m not finding a way to make it work.

It works fine if I run this code on iex -S mix:

{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "test_tb", show_sensitive_data_on_connection_error: true)
Postgrex.query!(pid, "SELECT id, name FROM company LIMIT 10", [])

But the same code on a mix task, I’m receiving this error:

16:14:37.725 [error] GenServer #PID<0.214.0> terminating
** (ArgumentError) unknown registry: Postgrex.TypeManager
    (elixir 1.11.4) lib/registry.ex:1276: Registry.key_info!/1
    (elixir 1.11.4) lib/registry.ex:569: Registry.lookup/2
    (postgrex 0.15.10) lib/postgrex/type_supervisor.ex:23: Postgrex.TypeSupervisor.locate/2
    (postgrex 0.15.10) lib/postgrex/protocol.ex:914: Postgrex.Protocol.bootstrap/3
    (postgrex 0.15.10) lib/postgrex/protocol.ex:635: Postgrex.Protocol.handshake/2
    (postgrex 0.15.10) lib/postgrex/protocol.ex:155: Postgrex.Protocol.connect_endpoints/6
    (db_connection 2.4.0) lib/db_connection/connection.ex:82: DBConnection.Connection.connect/2
    (connection 1.1.0) lib/connection.ex:622: Connection.enter_connect/5
Last message: nil
State: Postgrex.Protocol

My mix.env:

{:mongodb, "~> 0.5.1"},
{:postgrex, ">= 0.15.0"}

And I’m using elixir 1.11.4 and erlang/OTP 23

If anyone could help me, it would be great.

Got help from the postgrex repo. It was missing this line before using the lib:

Application.ensure_all_started(:postgrex)
1 Like