0

Trying to create tables without double quoting the name is resulting in a syntax error, only quoting it works:

# CREATE TABLE user (
email TEXT,
first_name TEXT,
last_name TEXT,
password TEXT,
plan TEXT,
gender gender
);
ERROR:  syntax error at or near "user"
LINE 1: CREATE TABLE user (
                     ^
# CREATE TABLE "user" (
email TEXT,
first_name TEXT,
last_name TEXT,
password TEXT,
plan TEXT,
gender gender
);
CREATE TABLE
# \dt
        List of relations
 Schema | Name | Type  |  Owner   
--------+------+-------+----------
 public | user | table | postgres
(1 row)

This is on PosgreSQL 9.5.5 shipping with Ubuntu 16.10:

PostgreSQL 9.5.5 on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, 64-bit

I know it's a bad idea to quote names in PostgreSQL but I can't get around this, what's wrong?

0

1 Answer 1

4

user is a reserved word in many RDBMS including PostgreSQL, so you should either escape it with double quotes " or pick another one.

P.S. As this identifier is a table name, it might be a good idea to use users instead of user.

Sign up to request clarification or add additional context in comments.

10 Comments

Usually you use plural in table names, so the table should be name users
Ah, how couldn't I've thought of that, dammit all.
I was trying to use Oracle's DBA naming standards. Looks like it won't work.
btw isnt good idea have a type gender and call the fieldname gender
@JuanCarlosOropeza, that's an enum.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.