英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论