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

**URL:** <https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760>\
**Category:** Questions / Help\
**Tags:** rebar3, erlang-otp, postgresql, pgo\
**Created:** [August 21, 2022, 12:45pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760 "2022-08-21T12:45:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![massivefermion](https://erlangforums.com/user_avatar/erlangforums.com/massivefermion/32/1598_2.png) [@massivefermion](https://erlangforums.com/u/massivefermion)\
**Post date:** [August 21, 2022, 12:45pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/1 "2022-08-21T12:45:03Z")

</div>

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](https://hex.pm/packages/pgo) and this is what I tried:

```erlang
  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:

```erlang
 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

---

<div class="post-metadata">

**Author:** ![heiheshang](https://erlangforums.com/user_avatar/erlangforums.com/heiheshang/32/1599_2.png) [@heiheshang](https://erlangforums.com/u/heiheshang)\
**Post date:** [August 21, 2022, 12:52pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/2 "2022-08-21T12:52:08Z")

</div>

why don’t you try epgsql - it’s more popular driver. [epgsql | Hex](https://hex.pm/packages/epgsql)

---

<div class="post-metadata">

**Author:** ![LeonardB](https://erlangforums.com/user_avatar/erlangforums.com/leonardb/32/1856_2.png) [@LeonardB](https://erlangforums.com/u/LeonardB)\
**Post date:** [August 21, 2022, 2:38pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/3 "2022-08-21T14:38:14Z")

</div>

Looks like you’re missing configuration options.

Have a look at the 1st example in the README

[https://github.com/erleans/pgo](https://github.com/erleans/pgo)

Looks like pool\_size and host are missing

---

<div class="post-metadata">

**Author:** ![starbelly](https://erlangforums.com/user_avatar/erlangforums.com/starbelly/32/209_2.png) [@starbelly](https://erlangforums.com/u/starbelly)\
**Post date:** [August 21, 2022, 6:51pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/4 "2022-08-21T18:51:20Z")

</div>

Confirmed :

```erlang
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}]}

```

> [@heiheshang](#):
>
> why don’t you try epgsql - it’s more popular driver. [epgsql | Hex](https://hex.pm/packages/epgsql)

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”.

---

<div class="post-metadata">

**Author:** ![massivefermion](https://erlangforums.com/user_avatar/erlangforums.com/massivefermion/32/1598_2.png) [@massivefermion](https://erlangforums.com/u/massivefermion)\
**Post date:** [August 22, 2022, 5:08am UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/5 "2022-08-22T05:08:28Z")

</div>

> [@heiheshang](#):
>
> why don’t you try epgsql - it’s more popular driver. [epgsql | Hex](https://hex.pm/packages/epgsql)

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

---

<div class="post-metadata">

**Author:** ![massivefermion](https://erlangforums.com/user_avatar/erlangforums.com/massivefermion/32/1598_2.png) [@massivefermion](https://erlangforums.com/u/massivefermion)\
**Post date:** [August 22, 2022, 5:09am UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/6 "2022-08-22T05:09:17Z")

</div>

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?

> [@starbelly](#):
>
> 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”.

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

---

<div class="post-metadata">

**Author:** ![fizfaz](https://erlangforums.com/user_avatar/erlangforums.com/fizfaz/32/1224_2.png) [@fizfaz](https://erlangforums.com/u/fizfaz)\
**Post date:** [August 22, 2022, 9:11am UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/7 "2022-08-22T09:11:49Z")

</div>

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:

```erlang
1> application:ensure_all_started(pgo).
{ok,[backoff,telemetry,pg_types,pgo]}

```

after this you can use it:

```erlang
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](https://rebar3.org/docs/configuration/dependencies/#runtime-dependencies) and it will be started automatically.

---

<div class="post-metadata">

**Author:** ![massivefermion](https://erlangforums.com/user_avatar/erlangforums.com/massivefermion/32/1598_2.png) [@massivefermion](https://erlangforums.com/u/massivefermion)\
**Post date:** [August 22, 2022, 2:59pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/8 "2022-08-22T14:59:14Z")

</div>

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

```erlang
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

---

<div class="post-metadata">

**Author:** ![starbelly](https://erlangforums.com/user_avatar/erlangforums.com/starbelly/32/209_2.png) [@starbelly](https://erlangforums.com/u/starbelly)\
**Post date:** [August 22, 2022, 3:30pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/9 "2022-08-22T15:30:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![massivefermion](https://erlangforums.com/user_avatar/erlangforums.com/massivefermion/32/1598_2.png) [@massivefermion](https://erlangforums.com/u/massivefermion)\
**Post date:** [August 22, 2022, 3:37pm UTC](https://erlangforums.com/t/getting-errors-connecting-to-postgresql-reason-for-termination-error-badmatch-undefined/1760/10 "2022-08-22T15:37:46Z")

</div>

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