Search code examples
javaspringpostgresqlspring-bootjpa

Springboot default data.sql not loading on startup


I've got this Spring Boot application that is working with a PostgreSQL database. The application-dev.properties file is as follows:

spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=root


spring.datasource.data=classpath:/sql/dev-data.sql


spring.jpa.properties.hibernate.default_schema=myschema

spring.jpa.show-sql=true
spring.jpa.database=POSTGRESQL

The database is installed in my development machine, and already initialized. The dev-data.sql file is in src/main/resources/sql and right now only deletes everything on a specific table (lets call it myschema.example_table) and inserts some registries. (This is intended to check the functionality works than for an actual use):

delete from myschema.example_table where id > 0;

insert into myschema.example_table (id, val1, val2) values (1, "Hello", "Petecander");
insert into myschema.example_table (id, val1, val2) values (2, "Goodbye", "Gromenauer");

My issue is that... nothing happens. Is as if there wasn't anything related to the dev-data.sql file at all. So, I'm completely at a loss here. I've been browsing for a while, and reading about a lot of different switches that can be enabled on the application.properties file, but nothing. Any idea?

EDIT: Just to provide a bit more of info that I've been asked down there: The application loads fine and can perform some basic CRUD stuff against the database (Just read stuff at the moment), so the application-dev.properties file seems that is being loaded right.


Solution

  • Hibernate will only execute data.sql for either create or create-drop

    Add any of the following property to your application properties file:

    • spring.jpa.hibernate.ddl-auto=create
    • spring.jpa.hibernate.ddl-auto=create-drop

    This is required along with the property spring.datasource.initialization-mode=always for external (not embedded) databases.