Using postgrex? Cannot start link?

Hello it’s me again with another question. I am working on a package to query a postgres database using postgrex, so I am looking to the postgrex docs: https://hexdocs.pm/postgrex/readme.html

but I cannot make it work. Even the first line:

{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres")
{:ok, #PID<0.69.0>}

I use my own credentials but I get this error:

** (EXIT from #PID<0.199.0>) shell process exited with reason: exited in: GenServer.call(DBConnection.Watcher, {:watch, DBConnection.ConnectionPool.Supervisor, {DBConnection.ConnectionPool.Pool, {#PID<0.203.0>, #Reference<0.1620969730.389414915.107009>, Postgrex.Protocol, [types: Postgrex.DefaultTypes, hostname: "localhost", username: "postgres", password: "postgres", database: "my_database"]}}}, :infinity)
    ** (EXIT) no process: the process is not alive or there's no process currently associated with the given name, possibly because its application isn't started

I become very much confused when the examples do not seem to work. Did I do this incorrectly? Is there some option I forget to pass?

Thank you very much for your wise assistance!

At this point, the process you spawned is very likely to be dead. The spawned process will execute the given function and exit after the function is done. Also you will get different process identifiers every time if you still respawn that one.

You can check by this if your process is still alive

Process.alive?(self())

Are you storing that PID somewhere?

Maybe I am seeing something you are not seeing? Try pasting that into iex:

{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres")

(using your own credentials)
I am storing the pid in the tuple. But I have no chance to use it. That one line fails. There is no chance to do anything with it. I want to try to run ANY query – maybe a SELECT, but this one example from the documentation seems not to work.

Okay then, have you tried calling Application.ensure_all_started!(:postgrex) beforehand? Or your own app name?

And does your app have its own supervision tree?

1 Like

try connect to the database with psql directly. maybe the credential has problem?

No, credentials are OK I tested with psql
But yes! Application.ensure_all_started(:postgrex) seems to work… I’m going to try something