Search code examples
javapostgresqlspring-bootspring-data

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


Solution

  • It should be:

    spring.jpa.generate-ddl = true
    

    in your application.properties.

    Or you can change this line:

    spring.jpa.hibernate.ddl-auto=update
    

    in

    spring.jpa.hibernate.ddl-auto=create
    

    More details: Spring Database initialization