痛点为 AI 基于上游原始证据的初步提炼;未包含额外中国市场检索。
开发者在 Spring Boot 项目中配置 PostgreSQL 数据库初始化时,期望通过 schema.sql 和 data.sql 文件自动创建表并插入初始数据,但实际运行时数据未插入或表结构不符合预期。用户尝试调整 spring.jpa.hibernate.ddl-auto 参数(none/update/create)后仍无法解决问题,说明现有文档或配置机制未能清晰解释 schema.sql/data.sql 与 JPA 的 ddl-auto 之间的交互规则。这种不确定性导致开发者反复试错、查阅资料,浪费大量时间在调试配置而非业务逻辑上,且由于缺乏明确的错误提示,问题难以定位,增加了开发初期的摩擦和心理负担。
Stack Overflow question
I am working on a Spring Boot project using PostgreSQL. My goal is to create the tables and insert initial data automatically when the application starts. I have this configuration in my `application.properties` file: spring.datasource.url=jdbc:postgresql://localhost:5432/progra3db spring.datasource.username=progra3 spring.datasource.password=progra3 spring.datasource.driver-class-name=org.postgresql.Driver spring.sql.init.mode=always spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true My `schema.sql` file has a table like this: CREATE TABLE IF NOT EXISTS nodes ( id BIGINT PRIMARY KEY, code VARCHAR(50), name VARCHAR(100), type VARCHAR(50), description TEXT, parent_id BIGINT ); My `data.sql` file has test data like this: INSERT INTO nodes (id, code, name, type, description, parent_id) VALUES (1, '1', 'Assets', 'GROUP', 'Company assets', NULL), (2, '1.1', 'Current Assets', 'GROUP', 'Short-term assets', 1), (3, '1.1.1', 'Cash', 'ACCOUNT', 'Cash available', 2); When I start the application, the project runs, but sometimes the data is not inserted or the table does not appear as expected. I tried changing `spring.jpa.hibernate.ddl-auto` between `none`, `update`, and `create`, but I am still confused about the correct configuration when using `schema.sql` and `data.sql`. What is the correct way to configure Spring Boot so that PostgreSQL initializes the schema and data from SQL files when the application starts?
Question details
- View count
- 44
- Answer count
- 0
- Last activity
- 2026/05/26
源数据· Raw Archive
- source
- Stack Overflow
- upstream_source
- stackoverflow
- upstream_item_id
- 79946890
- daily_ranking_item_id
- 790b4292-25ad-473d-9395-85493f977c00
- rank_date
- 2026-05-27
- rank
- 7
- name
- Why does Spring Boot not initialize my PostgreSQL data with schema.sql and data.sql?
- tagline
- java, postgresql, spring-boot, spring-data-jpa
- description
- I am working on a Spring Boot project using PostgreSQL. My goal is to create the tables and insert initial data automatically when the application starts. I have this configuration in my `application.properties` file: spring.datasource.url=jdbc:postgresql://localhost:5432/progra3db spring.datasource.username=progra3 spring.datasource.password=progra3 spring.datasource.driver-class-name=org.postgresql.Driver spring.sql.init.mode=always spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true My `schema.sql` file has a table like this: CREATE TABLE IF NOT EXISTS nodes ( id BIGINT PRIMARY KEY, code VARCHAR(50), name VARCHAR(100), type VARCHAR(50), description TEXT, parent_id BIGINT ); My `data.sql` file has test data like this: INSERT INTO nodes (id, code, name, type, description, parent_id) VALUES (1, '1', 'Assets', 'GROUP', 'Company assets', NULL), (2, '1.1', 'Current Assets', 'GROUP', 'Short-term assets', 1), (3, '1.1.1', 'Cash', 'ACCOUNT', 'Cash available', 2); When I start the application, the project runs, but sometimes the data is not inserted or the table does not appear as expected. I tried changing `spring.jpa.hibernate.ddl-auto` between `none`, `update`, and `create`, but I am still confused about the correct configuration when using `schema.sql` and `data.sql`. What is the correct way to configure Spring Boot so that PostgreSQL initializes the schema and data from SQL files when the application starts?
- votes_count
- -1
- comments_count
- 0
- created_at_on_source
- 2026-05-26T17:08:48.000Z
{
"stackoverflow": {
"score": -1,
"view_count": 44,
"is_answered": false,
"top_answers": [],
"answer_count": 0,
"accepted_answer_id": null,
"last_activity_date": 1779815328
}
}{
"stats": {
"score": -1,
"view_count": 44,
"is_answered": false,
"answer_count": 0,
"creation_date": 1779815328,
"last_edit_date": null,
"accepted_answer_id": null,
"last_activity_date": 1779815328
},
"api_wrapper": {
"backoff": null,
"has_more": true,
"page_size": 8,
"quota_max": 300,
"quota_remaining": 296
},
"question_id": 79946890,
"answer_fetch": {
"has_more": false,
"answers_fetched": 0,
"answer_page_size": 3
},
"snapshot_version": "stackoverflow_question_v1"
}{
"id": "1c534143-fdb2-4ecd-bd5e-6f926d308844",
"daily_ranking_item_id": "790b4292-25ad-473d-9395-85493f977c00",
"source": "stackoverflow",
"external_id": "79946890",
"fetched_at": "2026-05-26T22:02:05.726Z",
"question_raw": {
"body": "<p>I am working on a Spring Boot project using PostgreSQL. My goal is to create the tables and insert initial data automatically when the application starts.</p>\n<p>I have this configuration in my `application.properties` file:</p>\n<pre><code>spring.datasource.url=jdbc:postgresql://localhost:5432/progra3db\n\nspring.datasource.username=progra3\n\nspring.datasource.password=progra3\n\nspring.datasource.driver-class-name=org.postgresql.Driver\n\n\n\nspring.sql.init.mode=always\n\nspring.jpa.hibernate.ddl-auto=none\n\nspring.jpa.show-sql=true\n\nspring.jpa.properties.hibernate.format_sql=true\n</code></pre>\n<p>My `schema.sql` file has a table like this:</p>\n<pre><code>CREATE TABLE IF NOT EXISTS nodes (\n\n id BIGINT PRIMARY KEY,\n\n code VARCHAR(50),\n\n name VARCHAR(100),\n\n type VARCHAR(50),\n\n description TEXT,\n\n parent_id BIGINT\n\n);\n</code></pre>\n<p>My `data.sql` file has test data like this:</p>\n<pre><code>INSERT INTO nodes (id, code, name, type, description, parent_id) VALUES\n\n(1, '1', 'Assets', 'GROUP', 'Company assets', NULL),\n\n(2, '1.1', 'Current Assets', 'GROUP', 'Short-term assets', 1),\n\n(3, '1.1.1', 'Cash', 'ACCOUNT', 'Cash available', 2);\n</code></pre>\n<p>When I start the application, the project runs, but sometimes the data is not inserted or the table does not appear as expected.</p>\n<p>I tried changing `spring.jpa.hibernate.ddl-auto` between `none`, `update`, and `create`, but I am still confused about the correct configuration when using `schema.sql` and `data.sql`.</p>\n<p>What is the correct way to configure Spring Boot so that PostgreSQL initializes the schema and data from SQL files when the application starts?</p>\n",
"link": "https://stackoverflow.com/questions/79946890/why-does-spring-boot-not-initialize-my-postgresql-data-with-schema-sql-and-data",
"tags": [
"java",
"postgresql",
"spring-boot",
"spring-data-jpa"
],
"owner": {
"link": "https://stackoverflow.com/users/32767385/angello-escobar",
"user_id": 32767385,
"user_type": "registered",
"account_id": 46413819,
"reputation": 27,
"display_name": "Angello Escobar",
"profile_image": "https://www.gravatar.com/avatar/caa55eb37aee0e1515745ae2801f68d5?s=256&d=identicon&r=PG&f=y&so-version=2"
},
"score": -1,
"title": "Why does Spring Boot not initialize my PostgreSQL data with schema.sql and data.sql?",
"view_count": 44,
"is_answered": false,
"question_id": 79946890,
"answer_count": 0,
"creation_date": 1779815328,
"content_license": "CC BY-SA 4.0",
"last_activity_date": 1779815328
},
"answers_raw": [],
"tags_raw": [
"java",
"postgresql",
"spring-boot",
"spring-data-jpa"
],
"stats_raw": {
"score": -1,
"view_count": 44,
"is_answered": false,
"answer_count": 0,
"creation_date": 1779815328,
"last_edit_date": null,
"accepted_answer_id": null,
"last_activity_date": 1779815328
},
"selection_meta": {
"site": "stackoverflow",
"api_wrapper": {
"backoff": null,
"has_more": true,
"page_size": 8,
"quota_max": 300,
"quota_remaining": 296
},
"answer_fetch": {
"backoff": null,
"has_more": false,
"answers_fetched": 0,
"quota_remaining": 267,
"answer_page_size": 3
},
"snapshot_version": "stackoverflow_question_v1",
"selection_strategy": "tag_whitelist_unanswered_high_score_recent_active"
},
"created_at": "2026-05-26T22:02:06.078Z",
"updated_at": "2026-05-26T22:02:06.078Z"
}