首页 > 解决方案 > Spring Boot doesn't create tables in Postgres

问题描述

I am trying to generate schema tables based on entities. But tables are not created. What's wrong?

Entity example:

@Entity
@NoArgsConstructor
@Getter @Setter
public class User {
    @Id @GeneratedValue(strategy = GenerationType.AUTO)
    public Long id;

    public String username;
    public String password;

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(String username) {
        this(username, username + "pass");
    }
}

application.properties:

spring.datasource.url=jdbc:postgresql://localhost:5432/dbtest
spring.datasource.username=vssekorin
spring.datasource.password=
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update

Dependencies:

compile('org.springframework.boot:spring-boot-starter-data-jpa')
runtime('com.h2database:h2')
compile('org.telegram:telegrambots:3.6.1')
runtime('org.postgresql:postgresql')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')

Warning: GenerationTarget encountered exception accepting command : Error executing DDL via JDBC Statement

标签: javapostgresqlspring-bootspring-data

解决方案


它应该是:

spring.jpa.generate-ddl = true

在你的application.properties.

或者您可以更改此行:

spring.jpa.hibernate.ddl-auto=update

spring.jpa.hibernate.ddl-auto=create

更多细节:Spring 数据库初始化


推荐阅读