Getting errors connecting to postgresql - Reason for termination = error:{badmatch,undefined}

I’m starting to learn erlang and right now I’m trying to connect to postgresql but I keep getting errors that are not clear at all.
I used pgo and this is what I tried:

  pgo_pool:start_link(pgo_pool, #{user => "dev", password => "password", database => "dev"}).

But I get a huge crash report when I run it in rebar3 shell that starts with this:

=ERROR REPORT==== 21-Aug-2022::08:53:08.607321 ===
** State machine <0.210.0> terminating
** Last event = {internal,load}
** When server state = {ready,{data,pgo_pool,
#{database => “dev”,
password => “password”,user => “dev”},
** Reason for termination = error:{badmatch,undefined}
** Callback modules = [pgo_type_server]
** Callback mode = state_functions

which does not seem to have any indication of what I did wrong!

Then I tried:

 pgo:start_pool(pgo_pool, #{user => "dev", password => "password", database => "dev"}).

This time I got:>

** exception exit: {noproc,{gen_server,call,
#{database => “dev”,password => “password”,user => “dev”}]},
in function gen_server:call/3 (gen_server.erl, line 247)

again, no idea what it means!

Any idea what’s going on here?

why don’t you try epgsql - it’s more popular driver. epgsql | Hex

Looks like you’re missing configuration options.

Have a look at the 1st example in the README

Looks like pool_size and host are missing


Confirmed :

1> pgo:start_pool(postgres, #{pool_size => 10, host => "", database => "postgres", user => "postgres"}).
2> pgo:query("select 1", [], #{pool => postgres}).
#{command => select,num_rows => 1,rows => [{1}]}

I’m not sure popularity is a good metric to use when deciding on what lib to use. I’d say pgo is worth using and supporting and enhancing. My argument for pgo is the model (See the Why section in the README).


I do think a PR should be opened on PGO to validate options and emit a useful signal, in this case it would have been “required fields missing, x,y,z”.

1 Like

I did, but it returns a weird and confusing structure when I make a query.

1 Like

that’s really weird, I added host and pool_size but I’m still getting the same errors in the original post!
Is there something wrong with my erlang installation?

the docs say pool_size and host have default values, that’s why I was leaving them out!

1 Like

Maybe the pgo application is not running? If you only add it to your deps in rebar.config it will be available, but not started. In this case you need to start it manually:

1> application:ensure_all_started(pgo).

after this you can use it:

2> pgo:start_pool(default, #{pool_size => 5, host => "", database => "felix", user => "felix"}).

if this helps, you can add it to your .app.src-file as described in rebar3 runtime dependencies and it will be started automatically.


thanks @fizfaz (and everyone else who replied), that solved the issue.
But now there is another problem.
What I do now is open rebar3 shell, then run application:ensure_all_started(pgo). and then

pgo:start_pool(default, #{host=>"",pool_size=>5,database=>"dev",user=>"dev",password=>"dev"}).

which returns {ok, <pid>}. So the connection seems fine.
But when I try to run a query like pgo:query("select 1",[],#{pool=>default})., I get {error,none_available}.
Again, very opaque! No idea what’s wrong! What’s going on?

1 Like

That sounds like you’re configuring a non-default pool, but not querying it. Are you giving the pool a name other than default?


sorry, my bad. I wasn’t starting the postgres container before testing it. It works.
Thanks everyone