Reputation: 584
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.
Upvotes: 1
Views: 7326
Reputation: 81
I've tried many things, and in the end i found that the combination of
spring.jpa.hibernate.ddl-auto=none
and
spring.sql.init.mode=always
did the trick for me.
Upvotes: 0
Reputation: 406
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.
Upvotes: 4
Reputation: 21
For spring boot version 2.7, adding spring.sql.init.mode=always
to application properties will work.
Upvotes: 0
Reputation: 41
Looks like some of the suggestions are deprecated now.
spring.datasource.initialization-mode=always
Needs to be replaced with:
spring.sql.init.mode=always
Here is a picture of my application.properties settings for a local H2 database if this helps anybody: Spring H2 Settings
Upvotes: 4
Reputation: 1
For embedded database like h2 , adding below entries in application.properties worked for me.
spring.datasource.data=classpath:mysql.sql
Upvotes: 0
Reputation: 35
Did you try to name file data.sql? I think it could help https://www.baeldung.com/spring-boot-data-sql-and-schema-sql
Upvotes: 1