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

Hi
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”},
undefined}}
** 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,
[pgo_sup,
{start_child,[pgo_pool,
#{database => “dev”,password => “password”,user => “dev”}]},
infinity]}}
in function gen_server:call/3 (gen_server.erl, line 247)

again, no idea what it means!

Any idea what’s going on here?
Thanks

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

https://github.com/erleans/pgo

Looks like pool_size and host are missing

2 Likes

Confirmed :

1> pgo:start_pool(postgres, #{pool_size => 10, host => "127.0.0.1", database => "postgres", user => "postgres"}).
{ok,<0.238.0>}
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).

Edit:

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).
{ok,[backoff,telemetry,pg_types,pgo]}

after this you can use it:

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

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

2 Likes

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=>"127.0.0.1",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?
Thanks

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?

2 Likes

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

3 Likes