Spring Batch / Postgres : ERROR: relation "batch_job_instance" does not exist

Spring Batch / Postgres : ERROR: relation "batch_job_instance" does not exist

我正在尝试配置 Spring 批处理以使用 PostGres 数据库。我在我的 build.gradle.kts 文件中包含了以下依赖项:

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.postgresql:postgresql")

我的 application.yml 我的 Spring 批处理模块包含以下内容:

spring:
  datasource:
    url: jdbc:postgresql://postgres:5432/springbatchdb
    username: postgres
    password: root
    driverClassName: org.postgresql.Driver

docker-compose.yml

postgres:
    restart: always
    image: postgres:12-alpine
    container_name: postgres
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=springbatchdb
    ports:
     - "5432:5432"
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data

但是,当我尝试添加数据文件时,我在 SpringBatch Docker 容器和 PostGres 容器的日志中看到以下错误:

Spring批次:

<<< Exception in method: org.meanwhileinhell.spring.batch.server.SpringBatchController.handle Error Message: PreparedStatementCallback; bad SQL grammar [SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? and JOB_KEY = ?]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "batch_job_instance" does not exist

PostGres:

LOG:  database system is ready to accept connections
2021-01-08 09:54:56.778 UTC [56] ERROR:  relation "batch_job_instance" does not exist at character 39
2021-01-08 09:54:56.778 UTC [56] STATEMENT:  SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME =  and JOB_KEY = 
2021-01-08 09:55:27.033 UTC [56] ERROR:  relation "batch_job_instance" does not exist at character 39
2021-01-08 09:55:27.033 UTC [56] STATEMENT:  SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME =  and JOB_KEY = 

我可以看到 SB 服务器正在从我的元数据中获取 POSTGRES。

JobRepositoryFactoryBean     : No database type set, using meta data indicating: POSTGRES

在服务器启动期间配置初始数据库我缺少什么?

编辑:我已经尝试明确添加 spring.datasource.initialize=true,但没有任何变化。

请检查下面添加在 application.yml

spring.batch.initialize-schema: always

请检查是否添加了以下依赖项

<artifactId>spring-boot-starter-batch</artifactId>

您需要设置 spring.batch.initialize-schema=always 属性 告诉 Spring 引导自动创建 Spring 批处理表。有关详细信息,请参阅 Spring 引导参考文档的 Initialize a Spring Batch Database 部分。

对于已经设置了 spring.batch.initialize-schema=always 但仍然无法正常工作的任何人,还请确认您正在使用具有足够权限的用户连接到数据库,包括创建必要的表。

yaml 文件是

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driver-class-name=org.postgresql.Driver
spring.batch.jdbc.initialize-schema=always

gradle 依赖关系

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-batch'
    implementation 'org.projectlombok:lombok-maven-plugin:1.18.6.0'
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.batch:spring-batch-test'
}