# PostgreSQL module cannot connect

**URL:** https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916
**Category:** Beats
**Tags:** metricbeat
**Created:** [February 25, 2020, 6:51pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916 "2020-02-25T18:51:57Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![thealy](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thealy](https://discuss.elastic.co/u/thealy)
#### Post date: [February 25, 2020, 6:51pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/1 "2020-02-25T18:51:58Z")

</div>

We are running metricbeat-7.6.0 on a RedHat 7 system. The basic Metricbeat app is running and connecting with ES. However, the postgresql module is not connecting to the local database. The password, shown here as "redacted" works otherwise. It appears to be using the username as the database name when trying to connect:

```auto
Feb 25 13:46:30 bnlbox04 metricbeat: 2020-02-25T13:46:30.884-0500#011INFO#011module/wrapper.go:252#011Error fetching data for metricset postgresql.bgwriter: error in QueryStats: failed to obtain a connection with the database: pq: database "redacted" does not exist

```

Here are the postgresql.yml contents:

```auto
- module: postgresql
  metricsets:
    - database
    - bgwriter
    - activity
  period: 10s
  hosts: ["postgres://localhost:5432?sslmode=disable"]
  username: redacted
  #password:

```

Any suggestions what the problem connecting is?  
Thanks,  
Terry

---

<div class="post-metadata">

### Author: ![Mario\_Castro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mario_castro/32/35107_2.png) [@Mario\_Castro](https://discuss.elastic.co/u/Mario_Castro)
#### Post date: [February 25, 2020, 9:50pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/2 "2020-02-25T21:50:04Z")

</div>

Hi @thealy 🙂 What's your postgres version? It might be some version mismatch. I'm not sure if I understood correctly but did you use `username` AND `password` to connect? (even if it's an empty password).

I agree that it seems like it's using the username as the database to connect and that's totally unexpected

---

<div class="post-metadata">

### Author: ![thealy](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thealy](https://discuss.elastic.co/u/thealy)
#### Post date: [February 25, 2020, 10:07pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/3 "2020-02-25T22:07:00Z")

</div>

Hi Mario.  
The version is psql (PostgreSQL) 11.7  
The password on this test system is blank.  
-Terry

---

<div class="post-metadata">

### Author: ![Mario\_Castro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mario_castro/32/35107_2.png) [@Mario\_Castro](https://discuss.elastic.co/u/Mario_Castro)
#### Post date: [February 25, 2020, 10:11pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/4 "2020-02-25T22:11:01Z")

</div>

> [@thealy](#):
>
> - module: postgresql metricsets: - database - bgwriter - activity period: 10s hosts: ["postgres://localhost:5432?sslmode=disable"] username: redacted #password:

My question in different words: have you checked if it fails also with this?

```yaml
- module: postgresql
  metricsets:
    - database
    - bgwriter
    - activity
  period: 10s
  hosts: ["postgres://localhost:5432?sslmode=disable"]
  username: redacted
  password: ""

```

---

<div class="post-metadata">

### Author: ![thealy](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thealy](https://discuss.elastic.co/u/thealy)
#### Post date: [February 26, 2020, 2:15pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/5 "2020-02-26T14:15:27Z")

</div>

Hi Mario-  
OK, I just tried that variation and the result is the same:  
Error fetching data for metricset postgresql.activity: error in QueryStats: failed to obtain a connection with the database: pq: database "bnlboxread" does not exist  
-Terry

---

<div class="post-metadata">

### Author: ![Mario\_Castro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mario_castro/32/35107_2.png) [@Mario\_Castro](https://discuss.elastic.co/u/Mario_Castro)
#### Post date: [February 26, 2020, 2:29pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/6 "2020-02-26T14:29:24Z")

</div>

Greats! Thank you for trying. After your test I have done a quick test using Docker [https://hub.docker.com/\_/postgres](https://hub.docker.com/_/postgres) with version 11.7

I didn't manage to reproduce the problem so it might be something on your environment. Things I tried:

- Set `POSTGRES_PASSWORD` to empty when initializing: This simply kills the container because empty passwords are not supported by the init script.
- Use `POSTGRES_HOST_AUTH_METHOD=trust` and comment `password` entry in Metricbeat. This worked and data was retrieved as expected.

Can you provide some way to reproduce your issue?

Thanks! 🙂

---

<div class="post-metadata">

### Author: ![thealy](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thealy](https://discuss.elastic.co/u/thealy)
#### Post date: [February 27, 2020, 7:04pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/7 "2020-02-27T19:04:44Z")

</div>

Thanks Mario.  
Can you tell me where I set the POSTGRES\_HOST\_AUTH\_METHOD=trust statement?  
Thanks

---

<div class="post-metadata">

### Author: ![Mario\_Castro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mario_castro/32/35107_2.png) [@Mario\_Castro](https://discuss.elastic.co/u/Mario_Castro)
#### Post date: [February 28, 2020, 11:10am UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/8 "2020-02-28T11:10:45Z")

</div>

It comes in the Docker container of Postgres but if you have an ad-hoc deployment I'm not sure if that env variable exists.

It's all here: [https://www.postgresql.org/docs/current/auth-methods.html](https://www.postgresql.org/docs/current/auth-methods.html) (`trust` is the one that gets activated by `POSTGRES_HOST_AUTH_METHOD` AFAICS

---

<div class="post-metadata">

### Author: ![thealy](https://avatars.discourse-cdn.com/v4/letter/t/41988e/32.png) [@thealy](https://discuss.elastic.co/u/thealy)
#### Post date: [February 28, 2020, 2:45pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/9 "2020-02-28T14:45:45Z")

</div>

As a work around, I have asked the DB admin to add a password - which should have been there anyhow. Hopefully then it will connect.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 27, 2020, 2:45pm UTC](https://discuss.elastic.co/t/postgresql-module-cannot-connect/220916/10 "2020-03-27T14:45:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
