Question

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).

Was it helpful?

Solution

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

OTHER TIPS

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

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

Trailing underscore _

The SQL standard explicitly promises to never use a trailing underscore (U+005F LOW LINE) in any name, keyword, or reserved word. To see for yourself, search the spec for the word “underscore”.

So, to avoid conflicts with any of the over a thousand keywords and reserved words used by various database engines, I name all my database identifiers with a trailing underscore. (Yes, really, over a thousand keywords reserved — I counted them.)

Change this:

CREATE TABLE user ( … ) ;

… to this:

CREATE TABLE user_ ( … ) ;

I do this as a habit for all database names: schemas, tables, columns, indexes, etc.

I rest a bit easier knowing I’ve eliminated an entire class of bugs and problems caused by naming collisions.

As an extra benefit, this practice makes quite clear in documentation, email, and such when referring to a programming language variable named user versus the database column user_. Anything with a trailing underscore is obviously from the database side.

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.

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

CREATE SCHEMA my_schema;
CREATE TABLE my_schema.user(...);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top