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.