'Cant connect to database from springboot app: password authentication failed for user "user"

I am trying to connect spring boot app to the postgres on docker but I got org.postgresql.util.PSQLException: FATAL: password authentication failed for user "user" even if I use correct password(I belive so)

docker-compose.yml in main project dir:

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped
  pgadmin:
    container_name: pgadmin
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:[email protected]}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
  postgres:
  pgadmin:

application.yml in springboot app dir:

 server:
  port: 8080
spring:
  application:
    name: customer
  datasource:
    password: password
    url: jdbc:postgresql://localhost:5432/customer
    username: user
  jpa:
    hibernate:
      ddl-auto: create-drop
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect
        format_sql: true
    show-sql: true

pg_hba.conf in docker:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

Steps I've taken to create database:

Docker compose up -d

starting containers

go to http://localhost:5050/browser/# and enter master password

creating server

creating database

and when I try to run spring boot app I got

org.postgresql.util.PSQLException: FATAL: password authentication failed for user "user"

I tried to restart images and delete them. Please help to find the bug in my configuration.



Solution 1:[1]

If you add the following to you docker-compose.yml, it will probably work.

 POSTGRES_DB: customer

You did no create the "schema", and that's why you are getting the error.

The postgres image configuration with the fix:

postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      PGDATA: /data/postgres
      POSTGRES_DB: customer
    volumes:
      - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pringi