【发布时间】:2019-07-18 22:55:40
【问题描述】:
我正在使用 Spring Boot、Spring Data JPA 和 PostgreSQL。
想要将 User 实体持久化到数据库 db_example。
@Entity
@Table(name = "my_user") // user is a reserved keyword in PostgreSQL
public class User {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String name;
private String email;
// getters and setters
}
我的 application.properties 非常标准:
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:postgresql://localhost:5432/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
是否可以在 Spring Boot Application 启动时创建数据库?
就我而言,我得到了
org.postgresql.util.PSQLException: FATAL: database "db_example" does not exist
【问题讨论】:
-
您需要在您的 PostgreSQL 服务器上手动创建
db_example数据库,然后这应该可以工作 -
即使可以:不要那样做。创建数据库需要具有超级用户权限的帐户。并且您的应用程序应该不一开始就使用这样的数据库用户,这是一个巨大的安全风险。毕竟,这是您设置服务器时的一次性操作。使用默认数据库
postgres有什么问题? -
@a_horse_with_no_name,其实我习惯了MongoDB的这种行为,它可以自动创建数据库。我打算创建一些 Spring Boot 微服务,它将一些数据存储在 PostgreSQL 中的一个单独的数据库中。所以我想自动化这个过程。
-
这听起来有点偏执,但这只是我的意见,我不是专家,而是初学者。显然,出于生产目的,我完全同意,但加快开发过程是一件好事。我无法想象每次我更改实体 POJO 中的字段时都要求 DBA 对架构进行一些更改...
-
@Pijotrek:对于架构更改的受控部署,Liquibase 或 Flyway 等工具是更好的解决方案
标签: postgresql spring-boot spring-data-jpa