简体   繁体   中英

Postgres Overlap in System and Local DB Table Name

I ran into a strange issue where I have a user table in my local DB, which appears to overlap with the postgres system table user when I try to run SQL queries using Postico. While I am running queries against my db, I am returned a current_user column that maps to the username accessing the db. What should I change to my SQL to point to the local DB user table?

SQL commands I'm trying to run:

SELECT *
FROM user
;

Returned current_user: johndoe

UPDATE user
SET user.password = 'cryptopw', user.authentication_token = NULL
WHERE user.user_id = 210;

USER is one of the reserved key words for PostgreSQL . It is also reserved for SQL 2011, 2008 and 92.

From the documentation:

SQL distinguishes between reserved and non-reserved key words. According to the standard, reserved key words are the only real key words; they are never allowed as identifiers.

emphasis mine

That is, if you want to avoid trouble, don't use user as an identifier (that is: it shouldn't be the name of a table, or a column, or an index, or a constraint, or a function, ...).

If you still want to try:

As a general rule, if you get spurious parser errors for commands that contain any of the listed key words as an identifier you should try to quote the identifier to see if the problem goes away.

That is, try:

SELECT
    *
FROM
    "user" ;

and

UPDATE 
    "user"
SET 
    password = 'cryptopw',        -- not "user".password
    authentication_token = NULL   -- not "user". authentication_token
WHERE 
    "user".user_id = 210;         -- you can, but don't need "user"

I would actually rename the table, call it my_user (or application_user or something else), follow the SQL standard, and forget about having to quote identifiers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM