0

I am currently learning spring boot. From online example it shows setting up postgress database on docker and connecting with spring boot application running locally in my PC.

my docker-compose.yaml has following

services:
  db:
    container_name: postgres_container
    image: postgres
    environment:
      POSTGRES_USER: root
      POSTGRES_PASSWORD: root
      POSTGRES_DB: jobapp
    volumes:
      - postgres_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks:
      - postgres_network
    restart: unless-stopped

  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: [email protected]
      PGADMIN_DEFAULT_PASSWORD: root
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
      - pgadmin_data:/var/lib/pgadmin
    ports:
      - "5050:80"
    networks:
      - postgres_network
    restart: unless-stopped

networks:
  postgres_network:
    driver: bridge

volumes:
  postgres_data:
    name: postgres_data
  pgadmin_data:
    name: pgadmin_data

and application.properties has

spring.datasource.url=jdbc:postgresql://localhost:5432/jobapp
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

and pom.xml has

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <scope>runtime</scope>
</dependency>

I can successfully connect to pgadmin, and it has jobapp database. The containers are running successfully in docker.

when i try following docker command on command line it fail

psql -h localhost -p 5432 -U root -d jobapp 

Password for user root:
psql: error: connection to server at "localhost" (::1), port 5432 failed: FATAL:  password authentication failed for user "root"

but this command works fine

docker exec -it postgres_container psql -U root -d jobapp

Also when i try to connect from spring boot i get the error

unable to obtain isolated JDBC connection [FATAL: password authentication failed for user "root"] 

Can someone provide some assistance on this ?

1
  • Can anyone provide some help on this? Commented Dec 2, 2024 at 7:18

1 Answer 1

0

It's been so many months, maybe you've already found the answer to your question, but assuming that hasn't happened, I read in some documentation that in docker you should inform "postgres" in application.properties instead of "localhost" as the postgresql address.

Sign up to request clarification or add additional context in comments.

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Would that cause the issue described in the question, or prevent connections to the server entirely?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.