Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Spring Boot does not create the schema on MySQL or PostgreSQL

ErrorTables are created on H2 but not on MySQL or PostgreSQL

Short answer

Spring Boot only defaults ddl-auto to create-drop for an embedded database with no Flyway or Liquibase. For MySQL or PostgreSQL it defaults to none, so no tables are created. Set spring.jpa.hibernate.ddl-auto explicitly, or use one schema tool such as Flyway.

  • Applies to Spring Boot 3, Hibernate 6, MySQL 8.4, PostgreSQL 16
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Search the project for the settings that decide who creates the schema. What you find (or do not find) points to the cause below.

    grep -rn "ddl-auto\|sql.init\|flyway\|liquibase" src/main/resources/ pom.xml build.gradle 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1It worked on in-memory H2, and stopped when you switched the URL to MySQL or PostgreSQLddl-auto defaults to none outside embedded databasesFix
    2. 2A migration tool is in the build, and entities are never turned into tablesFlyway or Liquibase is on the classpathFix
    3. 3Startup fails in data.sql with a missing table, although ddl-auto is setschema.sql or data.sql runs before Hibernate has created the tablesFix

What does the error look like?

  • Tables are created on H2 but not on MySQL or PostgreSQL
  • No CREATE TABLE statements from Hibernate appear at startup

Cause 1 of 3

ddl-auto defaults to none outside embedded databases

1Check

Spring Boot identifies an embedded database from the connection type and JDBC URL. Only hsqldb, h2 and derby count. For anything else ddl-auto defaults to none.

grep -rn "datasource.url\|ddl-auto" src/main/resources/

2Fix

Set the value you want. update is common in development; the docs list none, validate, update, create and create-drop.

spring.jpa.hibernate.ddl-auto=update

3Verify

Restart; the tables appear.

mysql -e "SHOW TABLES" app

Confirmed by docs.spring.io

Did this fix it?

Cause 2 of 3

Flyway or Liquibase is on the classpath

1Check

Even on an embedded database, create-drop applies only when no schema manager (Flyway or Liquibase) has been detected. With one present the default is none and your migrations must create the tables.

mvn dependency:tree | grep -i "flyway\|liquibase"

2Fix

Put the CREATE TABLE statements in the migrations, and use one mechanism for the schema, not two. The docs advise against mixing several initialization technologies.

-- src/main/resources/db/migration/V1__create_users.sql
CREATE TABLE users (id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL);

3Verify

Start the app and list tables.

mysql -e "SHOW TABLES" app

Confirmed by docs.spring.io

Did this fix it?

Cause 3 of 3

schema.sql or data.sql runs before Hibernate has created the tables

1Check

Script-based DataSource initialization runs by default before any JPA EntityManagerFactory is created. By default it also runs only for embedded databases unless spring.sql.init.mode is always.

ls src/main/resources/*.sql

2Fix

To let scripts build on what Hibernate creates, defer the script run until after the EntityManagerFactory.

spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=always
spring.jpa.defer-datasource-initialization=true

3Verify

Restart; scripts run after the tables exist.

mysql -e "SELECT COUNT(*) FROM users" app

Confirmed by docs.spring.io

Did this fix it?

How do I stop it coming back?

  • Use none or validate in production and let migrations own the schema.
  • Do not assume tables exist when you move from an in-memory to a real database; Spring's docs warn about this.
  • Keep one schema mechanism per environment.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.