Skip to content

Instantly share code, notes, and snippets.

@pavankjadda
Last active October 7, 2020 23:03
Show Gist options
  • Save pavankjadda/6de44339461bb3462a331d4897e4dd25 to your computer and use it in GitHub Desktop.
Save pavankjadda/6de44339461bb3462a331d4897e4dd25 to your computer and use it in GitHub Desktop.
How to insert data in to H2 database during Spring Boot Application startup
  1. Create schema.sql file src/main/resources directory. This file contains all DDL statements
  2. Create data.sql file in src/main/resources directory. This file contains all DML statements(insert,update statements)
  3. Create the application.yml file with following config
spring:
  datasource:
    url: jdbc:h2:mem:db
    username: admin
    password: admin
    initialization-mode: always
    data: classpath:data.sql
    schema: classpath:schema.sql

  jpa:
    hibernate:
      ddl-auto: none

  h2:
    console:
      enabled: true
server:
  port: 8081

  1. In application.yml file make sure you set the following
  • spring.datasource.initialization-mode=always
  • spring.datasource.schema=classpath:schema.sql
  • spring.datasource.data=classpath:data.sql
  • And spring.jpa.hibernate.ddl-auto=none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment