Cannot create a database table named 'user' in PostgreSQL

PostgresqlHibernateIdentifierReserved Words

Postgresql Problem Overview


It seems PostgreSQL does not allow to create a database table named 'user'. But MySQL will allow to create such a table.

Is that because it is a key word? But Hibernate cannot identify any issue (even if we set the PostgreSQLDialect).

Postgresql Solutions


Solution 1 - Postgresql

user is a reserved word and it's usually not a good idea use reserved words for identifiers (tables, columns).

If you insist on doing that you have to put the table name in double quotes:

create table "user" (...);

But then you always need to use double quotes when referencing the table. Additionally the table name is then case-sensitive. "user" is a different table name than "User".

If you want to save yourself a lot of trouble use a different name. users, user_account, ...

More details on quoted identifiers can be found in the manual: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS

Solution 2 - Postgresql

It is possible to specify tablename with JPA with next syntax:

@Table(name="\"user\"")

Solution 3 - Postgresql

We had this same issue time ago, and we just changed the table name from user to app_user. Due to the use of Hibernate/JPA. We thought it would be easier this way. Hope this little fix will help someone else.

Solution 4 - Postgresql

You can create a table user in a schema other than public. The example:

CREATE SCHEMA my_schema;
CREATE TABLE my_schema.user(...);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChannaView Question on Stackoverflow
Solution 1 - Postgresqla_horse_with_no_nameView Answer on Stackoverflow
Solution 2 - PostgresqlNickView Answer on Stackoverflow
Solution 3 - PostgresqlSergio A.View Answer on Stackoverflow
Solution 4 - PostgresqlAlexElinView Answer on Stackoverflow