简体   繁体   English

PostgreSQL选择查询中的全限定表名

[英]Fully qualified table name in postgreSQL select query

Simple question, however google can't help in reasonable time. 简单的问题,但是Google在合理的时间内无法提供帮助。 Ok, I have user table in my_db database with id column. 好的,我的my_db数据库中的用户表具有id列。 I want to run very simple query 我想运行非常简单的查询

SELECT id FROM user;

but it fails. 但是失败了。

ERROR: column "id" does not exist LINE 1: SELECT id FROM user; 错误:“ id”列不存在第1行:从用户处选择ID;

Can you imagine? 你可以想象?

Ok, Running 好的,跑步

SELECT * FROM user;

outputs the list of internal postgresql database users, which is nothing to do with my users, it's data from completely another [internal] database. 输出内部postgresql数据库用户列表,这与我的用户无关,它完全来自另一个[内部]数据库。 However, connection with my_db was established. 但是,已建立与my_db的连接。

The following query can be rewritten as 以下查询可以重写为

SELECT id FROM my_db.public.user;

Where id is column, my_db is database, user is table name, public - is the schema. 其中ID为列,MY_DB是数据库, 用户表名, 公共 -是架构。 More about schemas: http://www.postgresql.org/docs/9.1/static/ddl-schemas.html So you don't have to rename the table name. 有关模式的更多信息: http : //www.postgresql.org/docs/9.1/static/ddl-schemas.html因此,您不必重命名表名。

user is an internal function (and a reserved word) returning the currently logged in user. user是返回当前登录用户的内部函数(和保留字)。

To use that as your own identifier, you need to quote it: 要将其用作您自己的标识符,您需要引用它:

select id 
from "user" 

or 要么

select id 
from public."user". 

But you should really avoid reserved words as table names (or any name that requires quoting the identifier) 但是,您应该避免使用保留字作为表名(或任何需要引用标识符的名称)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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