Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

How do Spring Boot's schema.sql and data.sql scripts work, which properties control them, and how do they interact with Hibernate's ddl-auto and Flyway?

At startup Spring Boot can run schema.sql and data.sql from the classpath; spring.sql.init.mode decides when, embedded (the default, only for embedded databases such as H2), always or never, and spring.sql.init.platform selects extra files such as schema-postgresql.sql. By default these scripts run before Hibernate initialises, unless spring.jpa.defer-datasource-initialization=true makes them run after Hibernate has created the schema. Because the scripts are not versioned, they suit tests, demos and embedded databases; when Flyway or Liquibase is present, the migrations should own the schema and script initialisation should not be used as well.

Why this question is asked

schema.sql and data.sql are often the first database feature a Spring Boot developer meets: drop a file into src/main/resources and the tables and rows appear. They are also a frequent source of confusion, because their behaviour depends on the database type, on Hibernate's settings and on whether a migration tool is present, and that behaviour changed in Spring Boot 2.5. Interviewers ask about them to check that we know what runs, in what order, and where these scripts belong. In this answer, we will see how script initialisation works, which properties control it, how platform-specific files work, how it interacts with Hibernate's ddl-auto and with Flyway, and when to use it at all.

1How Script Initialisation Works

When the application starts, Spring Boot looks for schema.sql and data.sql in the root of the classpath, normally src/main/resources. If it decides initialisation should run, it executes schema.sql first, to create tables, and then data.sql, to insert rows, against the application's DataSource. The statements run as they are: there is no history table, no checksum and no record that they ran.

That last point is the key to everything else. Because nothing remembers that the scripts ran, they run again on every start. On an in-memory database that is exactly right, since the database is empty each time. On a persistent database, a CREATE TABLE fails the second time unless it says IF NOT EXISTS, and an INSERT adds duplicate rows or fails on a unique key.

2The Properties That Control It

The spring.sql.init.* properties, introduced in Spring Boot 2.5, control the behaviour.

Properties
# When to run: embedded (default), always, never
spring.sql.init.mode=embedded
# Which files (defaults shown)
spring.sql.init.schema-locations=classpath:schema.sql
spring.sql.init.data-locations=classpath:data.sql
# Platform: also load schema-${platform}.sql and data-${platform}.sql (default: all)
spring.sql.init.platform=postgresql
# Behaviour on errors (default false: stop startup on the first failing statement)
spring.sql.init.continue-on-error=false

With the default mode, embedded, scripts run only when the DataSource is an embedded database such as H2, HSQLDB or Derby. Against PostgreSQL or MySQL nothing happens unless we set always. This default protects real databases from scripts that were written for a throwaway one. never switches the feature off completely, which is a sensible explicit choice in production.

3Platform-Specific Scripts

SQL often differs between databases, so Spring Boot can load scripts for one platform. With spring.sql.init.platform=postgresql, it runs schema.sql and schema-postgresql.sql, then data.sql and data-postgresql.sql, when they exist. The default platform value is all, which matches files named schema-all.sql and data-all.sql.

This lets a project keep, for example, an H2 version of the schema for one profile and a PostgreSQL version for another, by setting the platform per profile. It works, but notice what it means: we are maintaining two schemas by hand. That is a hint that these scripts are meant for simple cases, not for a real schema that evolves.

4How It Interacts With Hibernate's ddl-auto

Hibernate can also create the schema itself, controlled by spring.jpa.hibernate.ddl-auto. For an embedded database with no migration tool, Spring Boot sets ddl-auto to create-drop by default; otherwise the default is none. So there may be two things trying to set up the schema, and the order matters.

Properties
# Default since Spring Boot 2.5: scripts run BEFORE Hibernate initialises
# schema.sql creates tables -> Hibernate starts (ddl-auto) -> application ready
# A data.sql that inserts into tables Hibernate creates fails:
# Table "CUSTOMER" not found
# Let Hibernate create the tables first, then run data.sql to fill them
spring.jpa.defer-datasource-initialization=true
spring.jpa.hibernate.ddl-auto=create-drop
# Hibernate creates tables -> data.sql inserts rows -> application ready
Step throughStartup order with script initialisation
  1. Is spring.sql.init.mode satisfied?embedded by default
    YesScripts will runNoSkip schema.sql and data.sql
  2. defer-datasource-initialization=true?
    YesHibernate first, then scriptsNoScripts first, then Hibernate
  3. Run schema.sql, then data.sqlplus platform files
  4. Application readyno record that the scripts ran

4 steps in order. Play them, or step through them one at a time.

Notice the common surprise. Before Spring Boot 2.5, data.sql ran after Hibernate, so a project that relied on Hibernate creating the tables and data.sql filling them broke on upgrade. Setting spring.jpa.defer-datasource-initialization=true restores that order. It is a reasonable setup for demos and tests where Hibernate owns the schema and data.sql only provides sample rows; mixing a schema.sql with Hibernate also creating tables is usually a mistake, since the two definitions will drift.

5Why Migrations Should Own the Schema When Present

When Flyway or Liquibase is on the classpath, it manages the schema with a history, checksums and an order. Running schema.sql and data.sql as well gives the schema two owners. The scripts might create a table that a later migration also creates, or insert rows that a migration expects to insert, and since the scripts are unversioned, there is no telling which state any environment is in. Spring Boot's documentation advises against using script-based initialisation together with a migration tool, and deferred initialisation in particular is not supported with them.

So with a migration tool, reference data goes into migrations too, as versioned scripts that run once, and test data goes into test-only migration locations, such as an extra Flyway location activated only in a test profile, or into test setup code.

6Where schema.sql and data.sql Belong

Because they are not versioned, these scripts suit situations where the database starts empty every time: unit and integration tests on an embedded database, demos, prototypes and samples. They are not a tool for evolving a production schema. The first time a production table needs a new column, a schema.sql approach has no way to apply just that change to an existing database, which is exactly the problem Flyway and Liquibase solve. A good habit is to set spring.sql.init.mode=never in production configuration, so a stray data.sql in the jar can never run there.

7Follow-up questions

Why did my data.sql suddenly fail after upgrading Spring Boot?

Since Spring Boot 2.5, script initialisation runs before Hibernate creates the schema. If the project relied on Hibernate's ddl-auto to create tables and data.sql to fill them, data.sql now runs against tables that do not exist yet. Setting spring.jpa.defer-datasource-initialization=true restores the old order.

Can I use these scripts with a real database in tests?

Yes, with spring.sql.init.mode=always in the test profile, for example against a Testcontainers database. The scripts must be valid for that database, and if the project uses Flyway, it is better to let the migrations build the schema and use the scripts, or test code, only for test data.

What happened to spring.datasource.initialization-mode?

It was the older property for the same feature. Spring Boot 2.5 replaced spring.datasource.initialization-mode, schema and data with the spring.sql.init.* properties, and the old ones were later removed, so current applications should use the new names.

8Conclusion

In this answer, we saw that Spring Boot runs schema.sql and then data.sql from the classpath at startup when spring.sql.init.mode allows it, embedded by default, always or never, with platform files such as schema-postgresql.sql selected by spring.sql.init.platform. By default the scripts run before Hibernate initialises, and spring.jpa.defer-datasource-initialization=true reverses that. Since the scripts are unversioned and rerun on every start, they suit tests, demos and embedded databases, while Flyway or Liquibase should own the schema whenever they are present.

What to say in the interviewAbout 80 seconds aloud

Open with

Spring Boot can run schema.sql and then data.sql from the root of the classpath at startup, against the application's DataSource. Whether it does is controlled by spring.sql.init.mode: the default, embedded, runs them only for embedded databases like H2; always runs them for any database; never turns it off. spring.sql.init.platform adds platform-specific files, so with postgresql it also runs schema-postgresql.sql and data-postgresql.sql, and the locations, separator and continue-on-error can be configured too.

Then, if they want more

The ordering with Hibernate matters. Since Spring Boot 2.5 the scripts run before Hibernate initialises, so if Hibernate's ddl-auto creates the tables and data.sql fills them, I need spring.jpa.defer-datasource-initialization=true to run data.sql afterwards.

The scripts have no history and no checksums, so they run on every start, which is fine for an in-memory database but wrong for a persistent one, and they cannot apply incremental changes. So I use them for tests, demos and embedded databases.

When Flyway or Liquibase is present, the migrations should own the schema, and I would not use script initialisation as well; reference data goes into versioned migrations and test data into a test-only migration location or test setup. In production I set spring.sql.init.mode to never explicitly.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 2.5
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do Spring Boot's schema.sql and data.sql scripts work, which properties control...": https://scrivio-interview-library.netlify.app/databases/schema-migration/548af0dd96/