0

I went through the link: https://walkingtechie.blogspot.com/2018/12/execute-schema-and-data-sql-on-startup-spring-boot.html and Spring Boot - Loading Initial Data already, but still struggling to understand few things.

I'm looking to execute schema.sql at the start of Spring Boot application run, for that I use below configuration. I want everything to be performed on Postgres DB.

I've created schema.sql file place in sql/schema.sql folder. I expect spring Boot to pick up that file and run it. Is there any way if we can do it ? I am using Spring Boot version 2.1.6.RELEASE and Postgres 11.0.

spring:   
  datasource:
    url: jdbc:postgresql://localhost:5432/test?currentSchema=test
    username: postgres
    password: postgres
    platform: postgres
    schema:
    - classpath:sql/schema.sql
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    properties:
      hibernate: 
        dialect: org.hibernate.dialect.PostgreSQLDialect
        default_schema: test
        format_sql: true
        jdbc: 
          lob:
            non_contextual_creation: true

    show-sql: true

    hibernate:
      ddl-auto: none
PAA
  • 1
  • 46
  • 174
  • 282

1 Answers1

0

I was able to solve this issue my own by using below code.

User.java

@Data
@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    private String name;
}

DataSqlApplication.java

@SpringBootApplication
public class DataSqlApplication {

    public static void main(String[] args) {
        SpringApplication.run(DataSqlApplication.class, args);
    }
}

application.yml

---
spring:   
  datasource:
    url: jdbc:postgresql://localhost:5432/test?currentSchema=test
    username: postgres
    password: admin
    platform: postgres
    initialization-mode: always
    schema: 
    - classpath:sql/schema.sql
    data:
    - classpath:sql/data.sql
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect
    properties:
      hibernate: 
        default_schema: test
        format_sql: true
        jdbc: 
          lob:
            non_contextual_creation: true

    show-sql: true

    hibernate:
      ddl-auto: none
    generate-ddl: false
  profiles:
    active:
    - local
# Logging
logging:
  level:
    org.springframework.data: debug

enter image description here

PAA
  • 1
  • 46
  • 174
  • 282