# Unable to connect to PostgreSQL database – Peer authentication failed

**URL:** <https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771>\
**Category:** Support - Assistance technique\
**Tags:** server-admin\
**Created:** [9 June 2026 14:07 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771 "2026-06-09T14:07:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernando](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://community.dhis2.org/u/fernando)\
**Post date:** [9 June 2026 14:07 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/1 "2026-06-09T14:07:06Z")

</div>

Hi everyone,

I’m trying to connect DHIS2 to a PostgreSQL database named `test`, but I keep running into a connection issue. When I attempt to connect via `psql`, I get the following error:

paul@tracker:~$ lxc exec postgres – bash  
root@postgres:~# psql -U test -d test

psql: error: connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: FATAL: Peer authentication failed for user “test”

Here is some context about my setup:

**Containers:**

lxc list  
±---------±--------±-------------------±-----±----------±----------+  
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |  
±---------±--------±-------------------±-----±----------±----------+  
| test | RUNNING | 17x.x.x.x (eth0) | | CONTAINER | 0 |  
| dhis | RUNNING | 17x.x.x.x (eth0) | | CONTAINER | 0 |  
| postgres | RUNNING | 17x.x.x.x (eth0) | | CONTAINER | 0 |  
±---------±--------±-------------------±-----±----------±----------+

**DHIS2 `dhis.conf`:**

# Database connection for PostgreSQL

connection.dialect = org.hibernate.dialect.PostgreSQLDialect  
connection.driver\_class = org.postgresql.Driver  
connection.url = jdbc:postgresql://17x.x.x.x/viva  
connection.username = test  
connection.password = test123

**PostgreSQL databases:**

postgres=# \l  
List of databases  
Name | Owner | Encoding | Locale Provider | Collate | Ctype  
-----------±---------±---------±----------------±--------±--------  
test | app | UTF8 | libc | C.UTF-8 | C.UTF-8  
postgres | postgres | UTF8 | libc | C.UTF-8 | C.UTF-8  
template0 | postgres | UTF8 | libc | C.UTF-8 | C.UTF-8  
template1 | postgres | UTF8 | libc | C.UTF-8 | C.UTF-8

**`pg_hba.conf`:**

# “local” is for Unix domain socket connections only

local all all peer

# IPv4 local connections:

host all all 127.0.0.1/32 scram-sha-256

# IPv6 local connections:

host all all ::1/128 scram-sha-256

# Allow replication connections

local replication all peer  
host replication all 127.0.0.1/32 scram-sha-256  
host replication all ::1/128 scram-sha-256

# Custom connection for user ‘test’

host test test 17x.x.x.x/32 scram-sha-256

I suspect this may be related to the authentication method (`peer` vs `scram-sha-256`) in PostgreSQL, but I’m not sure how to fix it  
Has anyone faced a similar issue connecting DHIS2 to PostgreSQL when using peer authentication, and how did you resolve it?

Thanks in advance for any guidance!

---

<div class="post-metadata">

**Author:** ![jason](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/jason/32/32739_2.png) [@jason](https://community.dhis2.org/u/jason)\
**Post date:** [10 June 2026 06:41 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/2 "2026-06-10T06:41:35Z")

</div>

Try

`psql -U test -d -h 127.0.0.1 test`

---

<div class="post-metadata">

**Author:** ![fernando](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://community.dhis2.org/u/fernando)\
**Post date:** [10 June 2026 08:49 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/3 "2026-06-10T08:49:46Z")

</div>

Hi @jason,

Should I run this from the host paul@tracker:~$ or within postgres container?

> [@](#):
>
> psql -U test -d -h 127.0.0.1 test

Thanks in advance

---

<div class="post-metadata">

**Author:** ![jason](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/jason/32/32739_2.png) [@jason](https://community.dhis2.org/u/jason)\
**Post date:** [10 June 2026 09:19 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/4 "2026-06-10T09:19:34Z")

</div>

Inside the Postgres container.  
And noticed a typo. 😃

psql -U test -h 127.0.0.1 -d test

---

<div class="post-metadata">

**Author:** ![fernando](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://community.dhis2.org/u/fernando)\
**Post date:** [10 June 2026 09:47 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/5 "2026-06-10T09:47:01Z")

</div>

Hi @jason

Thanks again for your help.

I have one more question to better understand the `pg_hba.conf` setup:

In your suggestion you used `127.0.0.1` (IPv4 local connections), which matches this rule:

host all all 127.0.0.1/32 scram-sha-256

My question is: **when should I use the custom rule instead?**

host test test 17x.x.x.x/32 scram-sha-256

Specifically, in which cases is it appropriate to connect using `127.0.0.1` (loopback inside the container) versus using the container’s actual IP address (`17x.x.x.x`) with a dedicated database/user rule?

---

<div class="post-metadata">

**Author:** ![jason](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/jason/32/32739_2.png) [@jason](https://community.dhis2.org/u/jason)\
**Post date:** [10 June 2026 10:05 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/6 "2026-06-10T10:05:11Z")

</div>

Well, it depends on what you are trying to achieve. Your original question was based on trying to access Postgres from within the container using psql. If you need an external app (like DHIS2) to connect to the database from an external container, then you would need to use the IP address (e.g. 17x.x.x.x).

---

<div class="post-metadata">

**Author:** ![fernando](https://dhis2.b-cdn.net/letter_avatar_proxy/v4/letter/f/779978/32.png) [@fernando](https://community.dhis2.org/u/fernando)\
**Post date:** [10 June 2026 10:24 UTC](https://community.dhis2.org/t/unable-to-connect-to-postgresql-database-peer-authentication-failed/73771/7 "2026-06-10T10:24:58Z")

</div>

Hi @jason,

Thanks a lot for the clear explanation.

I appreciate your time and support.
