Docker-compose fails to create table on startup for Spring WebFluxPostgresql - Stack Overflow

admin2025-04-19  0

My application is running but the docker-compose is not creating the tables for me in my postgres docker container. At a loss for this at this point. Below is my relevant code

docker-compose

version: '3.8'

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    ports:
      - "5432:5432" # Optional, expose port for external access (e.g., pgAdmin)
    volumes:
      - postgres-data:/var/lib/postgresql/data # Persist data

  app:
    build:
      context: . # Assumes your Dockerfile is in the same directory
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/mydb
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: postgres
      SPRING_R2DBC_URL: r2dbc:postgresql://db:5432/mydb
      SPRING_R2DBC_USERNAME: postgres
      SPRING_R2DBC_PASSWORD: postgres
      SPRING_JPA_HIBERNATE_DDL_AUTO: create # Or validate, create, update, create-drop. Adjust for production.
    depends_on:
      - db

volumes:
  postgres-data:

application.properties

spring.application.name=my-project-service

spring.datasource.url=jdbc:postgresql://db:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.r2dbc.url=r2dbc:postgresql://db:5432/mydb
spring.r2dbc.username=postgres
spring.r2dbc.password=postgres

spring.jpa.hibernate.ddl-auto=create
spring.r2dbc.initialization-mode=always

I also have a schema.sql under my resources but it seems to be ignored also schema.sql

CREATE TABLE IF NOT EXISTS stocks (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

I have tried various configurations of docker-compose with no luck. If I manually add my table in postgres my code works correctly. I want it to create tables on start up and it is not doing it.

My application is running but the docker-compose is not creating the tables for me in my postgres docker container. At a loss for this at this point. Below is my relevant code

docker-compose

version: '3.8'

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    ports:
      - "5432:5432" # Optional, expose port for external access (e.g., pgAdmin)
    volumes:
      - postgres-data:/var/lib/postgresql/data # Persist data

  app:
    build:
      context: . # Assumes your Dockerfile is in the same directory
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/mydb
      SPRING_DATASOURCE_USERNAME: postgres
      SPRING_DATASOURCE_PASSWORD: postgres
      SPRING_R2DBC_URL: r2dbc:postgresql://db:5432/mydb
      SPRING_R2DBC_USERNAME: postgres
      SPRING_R2DBC_PASSWORD: postgres
      SPRING_JPA_HIBERNATE_DDL_AUTO: create # Or validate, create, update, create-drop. Adjust for production.
    depends_on:
      - db

volumes:
  postgres-data:

application.properties

spring.application.name=my-project-service

spring.datasource.url=jdbc:postgresql://db:5432/mydb
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.r2dbc.url=r2dbc:postgresql://db:5432/mydb
spring.r2dbc.username=postgres
spring.r2dbc.password=postgres

spring.jpa.hibernate.ddl-auto=create
spring.r2dbc.initialization-mode=always

I also have a schema.sql under my resources but it seems to be ignored also schema.sql

CREATE TABLE IF NOT EXISTS stocks (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255),
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

I have tried various configurations of docker-compose with no luck. If I manually add my table in postgres my code works correctly. I want it to create tables on start up and it is not doing it.

Share Improve this question asked Mar 5 at 23:15 Beartato327Beartato327 1172 silver badges7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

may be or not r2dbc not support Hibernate automatic ddl , you can add it into database container .

version: '3.8'

services:
  db:
    image: postgres:17
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydb
    ports:
      - "5432:5432"
    volumes:
      - postgres-data:/var/lib/postgresql/data
      - ./init-scripts:/docker-entrypoint-initdb.d 
volumes:
  postgres-data:

Besides you can also configurate it in spring like following ,

@Configuration
public class DbSchemaInitOnStartup {

    @Bean
    ConnectionFactoryInitializer initializer(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
        ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
        initializer.setConnectionFactory(connectionFactory);
        ResourceDatabasePopulator resource = new ResourceDatabasePopulator(new ClassPathResource("schema.sql"));
        initializer.setDatabasePopulator(resource);
        return initializer;
    }

}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745002927a279338.html

最新回复(0)