0

I have Spring Boot App with Postgres which I want to run with Docker and user and database should create automatically with environmental variables.

docker-compose.yml

version: '3'
services:
  microservice:
build: ./
image: someimage
ports:
  - "8080:8080"
environment:
  MEMORY_OPTS: "-Xmx512M"
  PG_DB_HOST: postgres
  PG_DB_PORT: 5432
  PG_DB_PASSWORD: postgres
depends_on:
  - postgres
  postgres:
image: postgres:10.4-alpine
environment:
  - POSTGRES_USER=postgres
  - POSTGRES_PASSWORD=postgres
  - POSTGRES_DB=db
ports:
  - 5432:5432

application.yml

spring:
config:
activate:
  on-profile: local
  jpa:
database-platform: org.hibernate.dialect.PostgreSQLDialect
hibernate:
  ddl-auto: update
  show-sql: true
datasource:
url: jdbc:postgresql://postgres:5432/db
username: postgres
password: postgres
liquibase:
change-log: classpath:/db/changelog/db.changelog-master.yaml

db/changelog/db.changelog-master.yaml

databaseChangeLog:
- logicalFilePath: db/changelog/db.changelog-master.yaml
-  preConditions:
-  runningAs:
     username:  postgres
- changeSet:
    id:  1
  author:  postgres
  changes:
    - createTable:
        tableName:  employees
        columns:
          - column:
              name:  EMPLOYEE_ID
              type:  int
              autoIncrement:  true
              constraints:
                primaryKey:  true
                nullable:  false
          - column:
              name:  FIRST_NAME
              type:  varchar(50)
          - column:
              name:  LAST_NAME
              type:  varchar(50)
              constraints:
                nullable:  false
          - column:
              name:  LOCATION
              type:  varchar(50)

And when I run docker compose up, I always get

Caused by: org.postgresql.util.PSQLException: FATAL: role "postgres" does not exist

or Caused by: org.postgresql.util.PSQLException: FATAL: database "db" does not exist

If I create manually user and database, it works, but as you understand, I need to do it automatically, I suppose some problems with env. variables.

  • How long does it take for the actual application to start? Is it enough time for postgres container to configure itself? What happens if you add a pause (sleep) before trying to access the database? – Yury Fedorov Nov 23 '21 at 08:48
  • After running `docker compose up` it takes approximately 10 second before I get such errors `Caused by: org.postgresql.util.PSQLException: FATAL: role "postgres" does not exist` – Дмитрий Фактов Nov 23 '21 at 08:56
  • @YuryFedorov And when I go to services > Docker > postgres , I see that my postgres is starting – Дмитрий Фактов Nov 23 '21 at 08:58
  • Can you try the "wait-for-it" solution from this answer? https://stackoverflow.com/a/64921431/4378400 It appears to me that the application or liquibase try to access postgres before it is healthy (being up doesn't mean it's healthy, it takes a few seconds after the container is running) – Yury Fedorov Nov 23 '21 at 09:03
  • @YuryFedorov Where should located `wait-for-postgres.sh` ? – Дмитрий Фактов Nov 23 '21 at 09:27
  • @ДмитрийФактов it should be used in `microservice` – Brian Destura Nov 24 '21 at 23:19

0 Answers0