使用 spring 引导从 H2 切换到 PostgreSQL

Switching from H2 to PostgreSQL with spring boot

我有一个 spring 引导应用程序在 H2 数据库上运行良好。如果我想切换到 postgresQL,我会收到错误消息。

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres

错误:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table if exists 
user cascade" via JDBC Statement
...
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "user"
...
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "create table user (id int8 not null, active int4 not null, first_name varchar(255), last_name varchar(255), password varchar(255), role varchar(255), username varchar(255), primary key (id))" via JDBC Statement

我认为这是因为 user 是 PostrgeSQL 中的保留字。

为了用这样的名称创建一个 table,请尝试引用它(即 create table "user"...)

如果您想要不同数据库之间的互操作性,将所有对象的名称包含在 'ANSI quotes' 中通常是个好主意。