从 H2 切换到使用 Spring Boot 的 PostgreSQL

huangapple go评论69阅读模式
英文:

Switching from H2 to PostgreSQL with spring boot

问题

我有一个在H2数据库上正常运行的Spring Boot应用程序。如果我想切换到PostgreSQL,就会出现错误。

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

错误:

org.hibernate.tool.schema.spi.CommandAcceptanceException: 通过JDBC语句执行DDL“drop table if exists 
user cascade”时出错
...
Caused by: org.postgresql.util.PSQLException: 错误:在"user"附近有语法错误
...
org.hibernate.tool.schema.spi.CommandAcceptanceException: 通过JDBC语句执行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))”时出错
英文:

I have a spring boot application working fine on a H2 DB. If I want to switch to postgresQL I get errors.

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

Errors:

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

答案1

得分: 13

我认为这是因为在PostrgeSQL中,“user”是一个保留词。

为了创建一个带有这种名称的表,试着对它进行引用(也就是说,使用 create table "user"...)

通常,如果你想在不同的数据库之间实现互操作性,将所有对象的名称都用“ANSI引号”括起来是一个不错的主意。

英文:

I think this is because user is a reserved word in PostrgeSQL.

In order to create a table with such name, try quote it (that is, create table "user"...)

Enclosing all the objects' names in 'ANSI quotes' is generally a good idea if you want an interoperability between different databases.

huangapple
  • 本文由 发表于 2020年5月5日 00:18:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61596812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定