5

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:-pgadmin4@pgadmin.org}
      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.

Ankit Ostwal
  • 1,033
  • 3
  • 14
  • 32
  • 1
    "Role "amigoscode" already exists" No it doesn't. PostgreSQL doesn't lie about this. If your persistent volume already has a db, then it gets reused, not recreated. If reused, then POSTGRES_USER gets ignored. – jjanes Jun 17 '22 at 16:57
  • @jjanes Can you let me know, what am i doing wrong here?. I am not able to understand this. – Ankit Ostwal Jun 18 '22 at 11:08
  • Have you solved it? – Hleb Shypula Jun 20 '22 at 19:54
  • @jjanes So what? Where is the solution? – Hleb Shypula Jun 20 '22 at 20:01
  • @AnkitOstwal guys, you have to debug this step-by-step, no one can guess what is going on in your setup. First, I would get into the DB and check if the user and role actually exist. Just google "psql list users/roles". If they do, it'd be worth paying more attention to the Spring config. And best to start with a clean env. – vladtkachuk Jun 23 '22 at 08:35
  • @AnkitOstwal I just created a real datasource on my localhost and everything works fine. We have no choice man – Hleb Shypula Jun 26 '22 at 20:16
  • @HlebShypula can you please show me the file or action you did? Didn't got you, real datasource? Help is appreciated. Thank you. – Ankit Ostwal Jul 04 '22 at 09:49
  • @AnkitOstwal 1) I created a real datasource with necessary databases on my local postgres (not via docker), 2) deleted pgadmin (I use DataGrip locally) and postgres from docker-compose.yml, 3) specified connection properties in application.yml. And everything works fine. Feel free to ask if you have any more questions – Hleb Shypula Jul 04 '22 at 12:07

4 Answers4

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

Abdelsalam Megahed
  • 1,281
  • 12
  • 13
  • I had problem for creating the database and that helped. (cause my local service was running) – UN..D Dec 16 '22 at 21:37
0

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.

RVKarmani
  • 43
  • 7
0

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
Pierre B.
  • 11,612
  • 1
  • 37
  • 58
-2

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.

vimuth
  • 5,064
  • 33
  • 79
  • 116
Filip
  • 1
  • 2