How to execute sql script after db schema generation but before application startup

I want to generate DB structure from my Java classes

jpa.generate-ddl: true jpa.ddl-auto: true

Also, I need to run SQL script before application will up because I have @PostConstruct methods where I use these data.

Can you show an example how to do it in Spring Boot?


A simple spring boot app with the required functionality can be found at. https://github.com/salilotr89/Spring-boot-postgres-dbinit

Spring JDBC has a DataSource initializer feature. Spring Boot enables it by default and loads SQL from the standard locations schema.sql and data.sql (in the root of the classpath). </b>

In addition Spring Boot will load the schema-${platform}.sql</b> and data-${platform}.sql</b> files (if present), where platform is the value of spring.datasource.platform, e.g. you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql etc.).

Spring Boot enables the fail-fast feature of the Spring JDBC initializer by default, so if the scripts cause exceptions the application will fail to start. The script locations can be changed by setting spring.datasource.schema and spring.datasource.data, and neither location will be processed if spring.datasource.initialize=false</b>.

To disable the fail-fast you can set spring.datasource.continue-on-error=true. This can be useful once an application has matured and been deployed a few times, since the scripts can act as ‘poor man’s migrations’ — inserts that fail mean that the data is already there, so there would be no need to prevent the application from running, for instance.

If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to "" (preferable) or "none". Whether or not you use ddl-auto=create-drop you can always use data.sql to initialize new data.


For Reference: Spring Boot - Loading Initial Data


All you need to do is place a file named <strong>data.sql</strong> in the resources folder where application.properties file reside.

Any query within this file will be executed before the start of the application and after the creation of your db.

To read more please check this link http://www.baeldung.com/spring-boot-data-sql-and-schema-sql


  • Using Recaptcha with EPiServer XForms
  • Do commands run from current directory in a shell script?
  • How can integers (keys & values) be added and retrieved from a NSDictionary
  • Loopback validation on Properties who's types are other Models
  • Consuming a web service with the Netbeans Platform
  • Getting errors while using neuralnet function
  • Zend Framework + Doctrine1.2 project structure with more modules
  • Nested projects in multiproject visual studio templates
  • Swift Initialization Rule Confusion
  • File extension of zlib zipped html page?
  • Retaining data after updating application
  • Examples of how to a STS in .Net 4.5 using WCF
  • How to pass solution folder as parameter in command line arguments (for debug)?
  • How to use Windows Media Foundation with UWP without a topology
  • Android Studio Can't Find tools.jar
  • Android Studio 1.3 RC3. Google Play services out of date. Requires 7571000 but found 6774470
  • How do I include a SWC in an AS2 Flash project?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • pyodbc doesn't report sql server error
  • JBoss External Properties Files in Classpath
  • Loading .coffee files via a view in Rails
  • Saving Changes After In-App Purchase Has Been Purchased
  • Create DicomImage from scratch using Dcmtk
  • htaccess add www if not subdomain, if subdomain remove www
  • x64 applications using gdi+: what are the consequences on performance?
  • Abort upload large uploads after reading headers
  • ActiveRecord query for a count of new users by day
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • java.lang.NoClassDefFoundError: com.parse.Parse$Configuration$Builder on below Lollipop versions
  • Jenkins: How To Build multiple projects from a TFS repository?
  • What is Eclipse's Declaration View used for?
  • PHP - How to update data to MySQL when click a radio button
  • Counter field in MS Access, how to generate?
  • javaw.exe and eclipse startup problems
  • HTML download movie download link
  • how does django model after text[] in postgresql [duplicate]
  • Java static initializers and reflection
  • How can i traverse a binary tree from right to left in java?
  • How to load view controller without button in storyboard?