Postgresql:“user”或附近的语法错误

时间:2022-06-01 12:35:16

This is probably a simple question, but I've spent an embarrassing amount of time trying to figure out what's wrong.

这可能是一个简单的问题,但我花了一大笔时间试图弄清楚什么是错的。

I'm trying to run a query on a table "user" containing two columns username and id.

我正在尝试对包含两列username和id的表“user”运行查询。

INSERT INTO user
   (username, id) 
VALUES
   ("user", 2)

I end up getting this error.

我最终得到了这个错误。

ERROR:  syntax error at or near "user"
LINE 1: INSERT INTO user
                    ^
********** Error **********

ERROR: syntax error at or near "user"
SQL state: 42601
Character: 13

Here is the table creation for reference

这是表创建供参考

-- Table: public."user"

-- DROP TABLE public."user";

CREATE TABLE public."user"
(
    id integer NOT NULL,
    username text COLLATE pg_catalog."default",
    CONSTRAINT user_pkey PRIMARY KEY (id)
)
WITH (
    OIDS = FALSE
)
TABLESPACE pg_default;

ALTER TABLE public."user"
    OWNER to postgres;

1 个解决方案

#1


3  

In Postgres user is a reserved SQL keyword. You should avoid naming your tables using reserved keywords. As a workaround here, you can place your table name in double quotes when referring to it:

在Postgres中,user是一个保留的SQL关键字。您应该避免使用保留关键字命名表。作为解决方法,您可以在引用时将表名放在双引号中:

INSERT INTO "user"
   (username, id) 
VALUES
   ('user', 2)

I also switched to using single quotes for string literals. This helps to distinguish the use of double quotes from single ones.

我也转而使用单引号作为字符串文字。这有助于区分双引号与单引号的使用。

#1


3  

In Postgres user is a reserved SQL keyword. You should avoid naming your tables using reserved keywords. As a workaround here, you can place your table name in double quotes when referring to it:

在Postgres中,user是一个保留的SQL关键字。您应该避免使用保留关键字命名表。作为解决方法,您可以在引用时将表名放在双引号中:

INSERT INTO "user"
   (username, id) 
VALUES
   ('user', 2)

I also switched to using single quotes for string literals. This helps to distinguish the use of double quotes from single ones.

我也转而使用单引号作为字符串文字。这有助于区分双引号与单引号的使用。