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
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.Your stack defines variables
POSTGRES_USER
andPOSTGRES_PASSWORD
with a volumepostgres
.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 dataIt’s likely you already ran
postgres
container with this volume, in which case you need to delete it then re-create it, for example:Then re-create your container and volumes (and if necessary other components):
Please open pgAdmin, login to postgreSQL and create new Datebase.
Creating new database
Then name "amigoscode" and you good to go.
amgigoscode database
It worked for me just fine.
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