skip to Main Content

I am a newbie to SpringBoot. I am trying to create a spring boot application which i am running using docker. when i run this app, i get the following error

org.postgresql.util.PSQLException: FATAL: role "amigoscode" does not exist

I don’t have any hint, as i am not able to trace this error. Role "amigoscode" already exists. I am attaching below the application.yml and docker-compose.yml

application.yml

server:
  port: 8080

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

docker-compose.yml

services:
  postgres:
    container_name: postgres
    image: postgres
    environment:
      POSTGRES_USER: amigoscode
      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:

Can you please guide me, what i might be doing wrong here? I have referred other similar question here, but none of them solves my issue. Thank you.

4

Answers


  1. When you’re running the Postgres container and passing POSTGRES_USER as an environment variable, an initialization script runs to create the user. However, if the folder (postgres volume) already contains data from older runs, the initialization is skipped and the user is not created. Try using a new volume and check the logs while the postgres container is starting.

    Login or Signup to reply.
  2. Your stack defines variables POSTGRES_USER and POSTGRES_PASSWORD with a volume postgres. postgres container will create these users only the first time it’s started, but is this volume has already been create before, it will re-use existing data

    It’s likely you already ran postgres container with this volume, in which case you need to delete it then re-create it, for example:

    # WARNING: this will delete all containers and volumes
    # including pg_data volume and pgadmin volume
    # make sure to make a backup if needed
    docker-compose down -v 
    

    Then re-create your container and volumes (and if necessary other components):

    docker-compose up -d
    
    Login or Signup to reply.
  3. Please open pgAdmin, login to postgreSQL and create new Datebase.

    Creating new database

    1

    Then name "amigoscode" and you good to go.

    amgigoscode database

    2

    It worked for me just fine.

    Login or Signup to reply.
  4. Additional to hints above, make sure that Postgres is not running on the local machine out of Docker because it might conflict with the Postgres instance in Docker

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search