Spring Boot supports multiple strategies for initializing a database on startup—from simple SQL scripts to full migration tools like Flyway and Liquibase. Choosing the right approach depends on your database type, environment, and whether you need versioned, repeatable migrations. This guide walks you through each strategy with concrete configuration examples.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
Use a single initialization mechanism. Mixing
schema.sql/data.sql scripts with Flyway or Liquibase is not recommended and support for combining them will be removed in a future release.Run schema.sql and data.sql on startup
Spring Boot automatically loads schema scripts fromoptional:classpath*:schema.sql and data scripts from optional:classpath*:data.sql at startup.
application.yaml
optional: prefix (present by default) means startup succeeds even when the files are absent. Remove it to fail fast on missing scripts.
By default, SQL initialization only runs against embedded in-memory databases. Set spring.sql.init.mode=always to run scripts against any database type, or never to disable initialization entirely.
Initialization scripts support
-- for single-line comments and /* */ for block comments. Other comment formats are not supported.spring.sql.init.platform. Spring Boot then processes schema-${platform}.sql and data-${platform}.sql files:
application.yaml
Use Flyway for migrations
Flyway provides versioned, repeatable database migrations with audit history. Follow these steps to set it up:Add the Flyway starter
Add
spring-boot-starter-flyway to your project. For database-specific features, also add the matching Flyway module:pom.xml
Create migration scripts
Place versioned migration scripts in
src/main/resources/db/migration. Name them using the pattern V<VERSION>__<NAME>.sql, where <VERSION> is an underscore-separated version number:V1__create_users_table.sql
V2__add_email_column.sql
Configure migration locations
By default, Flyway looks in Use the With this setting, Flyway uses
classpath:db/migration. Customize the location or add multiple paths using spring.flyway.locations:application.yaml
{vendor} placeholder to switch to database-specific directories automatically:application.yaml
db/migration/postgresql for PostgreSQL, db/migration/mysql for MySQL, and so on.Configure Flyway behavior
Control whether Flyway validates the migration checksum on startup and other common options:
application.yaml
Use a dedicated Flyway DataSource (optional)
By default, Flyway uses the auto-configured primary Setting either
DataSource. To point Flyway at a separate database, set its connection properties directly:application.yaml
spring.flyway.url or spring.flyway.user is sufficient to make Flyway use its own DataSource. Any unset property falls back to the equivalent spring.datasource.* value.Use Liquibase for migrations
Addspring-boot-starter-liquibase to your classpath. Liquibase runs automatically during application startup and before tests:
pom.xml
db/changelog/db.changelog-master.yaml. Configure the path with spring.liquibase.change-log:
application.yaml
db.changelog-master.yaml
DataSource for Liquibase, set its connection properties directly:
application.yaml
It is not possible to use two different initialization mechanisms for the same environment—for example, Liquibase for the application and JPA DDL for tests. Use the
spring.liquibase.enabled property with Spring profiles to control this.Initialize a Spring Batch database
Spring Batch ships pre-packaged SQL scripts for most database platforms. Spring Boot auto-detects the database type and runs those scripts. For embedded databases this happens by default. Enable it for any database type:application.yaml
Use test-only migrations
- Flyway
- Liquibase
Place test-specific migration scripts in You can also use profile-specific locations to run certain migrations only when a particular Spring profile is active. In
src/test/resources/db/migration. These files are executed after production migrations but only when running tests. They are not packaged in the application jar.Example test data script:src/test/resources/db/migration/V9999__test-data.sql
application-dev.yaml:application-dev.yaml
Control initialization order
Spring Boot automatically detects beans that initialize a database and beans that depend on initialization, and configures the dependency chain accordingly. The following types are detected as database initializers:DataSourceScriptDatabaseInitializerEntityManagerFactoryFlyway/FlywayMigrationInitializerR2dbcScriptDatabaseInitializerSpringLiquibase
AbstractEntityManagerFactoryBeanDSLContext(jOOQ)JdbcClientJdbcOperationsNamedParameterJdbcOperations
@Bean method with @DependsOnDatabaseInitialization to register it in the dependency chain.
To have a custom third-party initializer detected automatically, register an implementation of DatabaseInitializerDetector in META-INF/spring.factories.