Scrivio Interview library
DebuggingIntermediate7 min read · updated 12 November 2026

After moving a service from H2 to PostgreSQL in staging, startup fails because data.sql inserts duplicate rows on every restart, and in another service data.sql did not run at all. Explain both behaviours.

Both behaviours come from spring.sql.init.mode. In the first service it is set to always, so data.sql runs on every startup, and against a PostgreSQL database that keeps its data between restarts, plain INSERT statements add the same rows again or fail on a unique constraint. In the second service the mode was left at its default, embedded, which only runs scripts against in-memory databases such as H2, so against PostgreSQL the script is silently skipped.

Why this question is asked

Moving from H2 to a real database is one of the first things a service does on its way to production, and it is exactly when hidden assumptions in startup scripts come out. With H2 in memory, every start begins with an empty database, so a script that blindly inserts rows works perfectly. PostgreSQL remembers. Interviewers like this question because it checks whether we know how Spring Boot decides to run SQL scripts, and whether we know where seed data really belongs. In this answer, we will recap how script-based initialisation works, explain the duplicate rows, explain the script that never ran, fix both properly, and see why Flyway and continue-on-error change the picture.

1A Quick Recap of SQL Initialisation in Spring Boot

Spring Boot can run two plain SQL files when the application starts: schema.sql, which usually holds CREATE TABLE statements, and data.sql, which holds INSERT statements. It looks for them on the classpath, so they normally live in src/main/resources. This feature is called script-based initialisation, and it is controlled by properties under spring.sql.init.

The most important of these is spring.sql.init.mode. It has three values. embedded, the default, runs the scripts only when the DataSource points at an embedded database, meaning one that runs inside our own JVM such as H2, HSQLDB or Derby. always runs them against any database on every start. never switches the feature off. Notice that there is no value that means run once. Spring Boot keeps no record of whether a script already ran; it simply executes the file again on each start when the mode allows it.

Step throughHow Spring Boot decides to run data.sql
  1. Read spring.sql.init.modedefault is embedded
  2. Mode is never?
    YesSkip the scriptsNoContinue
  3. Mode is always?
    YesRun schema.sql and data.sqlNoMode is embedded
  4. Is the database embedded (H2, HSQLDB, Derby)?
    YesRun the scriptsNoSkip silently

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

There is no run-once mode: whenever the scripts run, they run in full.

2Why the First Service Inserts Duplicates on Every Restart

In the first service somebody set the mode to always, probably because the scripts stopped running after the move to PostgreSQL. That made them run again, but now on every start. Here is a typical data.sql written in the H2 days.

SQL
-- data.sql
INSERT INTO country (code, name) VALUES ('DE', 'Germany');
INSERT INTO country (code, name) VALUES ('FR', 'France');
-- first start: 2 rows inserted
-- second start, no unique constraint on code: 4 rows, each country twice
-- second start, code is the primary key: ERROR: duplicate key value violates unique constraint "country_pkey"
-- and the application fails to start

With H2 in memory this was never a problem, because the database disappeared when the JVM stopped. PostgreSQL keeps the rows, so the second start either quietly doubles the data or, if the table has a primary key or unique constraint, throws an error that stops the application context from starting. Both outcomes are what we see in staging.

3Why the Second Service Never Ran Its Script

The second service kept the default mode, embedded. Against H2 the scripts ran on every start, so the team got used to seeing their seed data appear. After switching the URL to jdbc:postgresql://..., Spring Boot sees a database that is not embedded and skips the scripts without failing. The tables stay empty, and the first symptom is usually a feature that cannot find its reference data.

There are two other reasons worth checking when a script does not run. If the file is named data-postgresql.sql, it only runs when spring.sql.init.platform is set to postgresql. And with JPA, if the script expects tables that Hibernate creates through ddl-auto, Spring Boot runs scripts before Hibernate unless spring.jpa.defer-datasource-initialization=true is set. For PostgreSQL in staging, though, the embedded default is by far the most common cause.

4Fix 1: Make the Seed Script Idempotent

If we keep data.sql for now, it must be idempotent, which means running it twice leaves the database in the same state as running it once. PostgreSQL makes this easy with ON CONFLICT.

SQL
INSERT INTO country (code, name) VALUES ('DE', 'Germany')
ON CONFLICT (code) DO NOTHING; -- first start: 1 row, later starts: 0 rows
-- or keep the name current when the script changes it
INSERT INTO country (code, name) VALUES ('FR', 'France')
ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name;

ON CONFLICT needs a unique constraint or primary key on the column it names, so the fix also forces us to declare what makes a row unique, which the table should have had anyway. This stops the duplicates, but it is still a workaround: the script runs on every start in every environment, and it gives us no history of what changed and when.

5Fix 2: Move Seed Data Into Versioned Migrations

The better fix is to treat reference data like schema and put it into Flyway or Liquibase migrations. A migration tool keeps a history table in the database, flyway_schema_history for Flyway, and runs each versioned script exactly once, in order. That is precisely the run-once behaviour data.sql lacks.

SQL
-- src/main/resources/db/migration/V3__seed_countries.sql (runs once, recorded in flyway_schema_history)
INSERT INTO country (code, name) VALUES ('DE', 'Germany'), ('FR', 'France');
-- R__country_names.sql (repeatable: runs again only when its checksum changes)
INSERT INTO country (code, name) VALUES ('DE', 'Germany'), ('FR', 'France')
ON CONFLICT (code) DO UPDATE SET name = EXCLUDED.name;
# application.yml
spring.sql.init.mode: never

When Flyway is on the classpath, data belongs in its migrations, either as a versioned script that runs once or as a repeatable migration whose name starts with R__ and which Flyway reapplies whenever the file content changes. Spring Boot's documentation advises against mixing plain scripts with Flyway or Liquibase, because the two mechanisms do not know about each other and the order in which they run is easy to get wrong, so we set the init mode to never once the migration exists.

6Why continue-on-error Should Stay False

A tempting shortcut is spring.sql.init.continue-on-error=true. It tells Spring Boot to log a failing statement and carry on. The duplicate-key error goes away and the application starts, but the reason it starts is that every error in the script is now ignored, including a typo in a new INSERT or a column that no longer exists. The default is false, and it should stay false, so that a broken script stops the start where we can see it rather than leaving half-seeded data behind.

7Follow-up questions

Does schema.sql have the same problem as data.sql?

Yes. With mode always, schema.sql runs on every start too. A plain CREATE TABLE fails the second time because the table exists, so people add IF NOT EXISTS, which hides drift because a changed definition is never applied. That is another reason to move schema into migrations, which apply each change once and record it.

Is mode always ever acceptable?

It is reasonable in tests or local development against a database that is recreated each time, for example a Testcontainers PostgreSQL started fresh for a test run, where every start really is a first start. For a database that persists between restarts, versioned migrations are the right tool.

How would you notice this before staging?

Run integration tests against the same database engine as production, for example PostgreSQL in Testcontainers, and restart the application context twice against the same container in one test. The duplicate rows or the unique-constraint failure appear immediately, and the skipped script shows up as an empty table assertion.

8Conclusion

In this answer, we saw that both behaviours come from spring.sql.init.mode. The default embedded mode runs scripts only against in-memory databases, so the second service silently skipped them against PostgreSQL. The always mode runs them on every start, so the first service duplicated rows or failed on a unique constraint once the database kept its data. Idempotent INSERT ... ON CONFLICT statements stop the damage, versioned or repeatable Flyway migrations are the real home for seed data, and continue-on-error should stay false so failures stay visible.

What to say in the interviewAbout 90 seconds aloud

Open with

Both come from spring.sql.init.mode. The default is embedded, which means Spring Boot only runs schema.sql and data.sql when the DataSource is an embedded database like H2. So in the second service, as soon as the URL pointed at PostgreSQL, the scripts were skipped without any error, and the tables stayed empty.

Then, if they want more

In the first service someone set the mode to always, which runs the scripts on every startup. With H2 in memory that was fine because each start had an empty database, but PostgreSQL keeps the rows, so plain INSERTs either duplicate the data or hit the primary key and fail the startup.

Spring Boot has no run-once memory for these scripts. The quick fix is to make the seed idempotent with INSERT ... ON CONFLICT DO NOTHING, which also means declaring a proper unique key.

The real fix is to move seed and reference data into Flyway migrations: a versioned V script runs exactly once and is recorded in the history table, and a repeatable R script reapplies when its content changes. Then I set the init mode to never so the two mechanisms are not mixed.

I would also keep continue-on-error false, because turning it on would make the app start while hiding every broken statement in the script. And to catch this earlier, I would run integration tests on PostgreSQL in Testcontainers rather than H2.

About this answer

Last updated
12 November 2026
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 "After moving a service from H2 to PostgreSQL in staging, startup fails because data.sql...": https://scrivio-interview-library.netlify.app/databases/schema-migration/72073e24bc/