【问题标题】:Postgres psql select result doesn't list all columns and selecting a column says it doesn't exist [duplicate]Postgres psql选择结果没有列出所有列并且选择一列说它不存在[重复]
【发布时间】:2020-05-21 16:26:09
【问题描述】:

数据库:postgres (PostgreSQL) 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1)

user@my-machine:~$ psql -U gogs -h localhost -W
Password for user gogs: 
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

gogs=> \l
                              List of databases
   Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges   
-----------+----------+----------+---------+---------+-----------------------
 gogs      | gogs     | UTF8     | C.UTF-8 | C.UTF-8 | 
 postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
 template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
 template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
           |          |          |         |         | postgres=CTc/postgres
(4 rows)

gogs=> \c gogs
Password for user gogs: 
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
You are now connected to database "gogs" as user "gogs".
gogs=> select * from user
gogs-> ;
 user 
------
 gogs
(1 row)

谁能解释一下为什么查询没有显示查询结果中的所有列,因为列出列我可以看到用户表中有很多列:

gogs=> \d+ user
                                                               Table "public.user"
        Column        |          Type           | Collation | Nullable |             Default              | Storage  | Stats target | Description 
----------------------+-------------------------+-----------+----------+----------------------------------+----------+--------------+-------------
 id                   | bigint                  |           | not null | nextval('user_id_seq'::regclass) | plain    |              | 
 lower_name           | character varying(255)  |           | not null |                                  | extended |              | 
 name                 | character varying(255)  |           | not null |                                  | extended |              | 
 full_name            | character varying(255)  |           |          |                                  | extended |              | 
 email                | character varying(255)  |           | not null |                                  | extended |              | 
 passwd               | character varying(255)  |           | not null |                                  | extended |              | 
 login_source         | bigint                  |           | not null | 0                                | plain    |              | 
 login_name           | character varying(255)  |           |          |                                  | extended |              | 
 type                 | integer                 |           |          |                                  | plain    |              | 
 location             | character varying(255)  |           |          |                                  | extended |              | 
 website              | character varying(255)  |           |          |                                  | extended |              | 
 rands                | character varying(10)   |           |          |                                  | extended |              | 
 salt                 | character varying(10)   |           |          |                                  | extended |              | 
 created_unix         | bigint                  |           |          |                                  | plain    |              | 
 updated_unix         | bigint                  |           |          |                                  | plain    |              | 
 last_repo_visibility | boolean                 |           |          |                                  | plain    |              | 
 max_repo_creation    | integer                 |           | not null | '-1'::integer                    | plain    |              | 
 is_active            | boolean                 |           |          |                                  | plain    |              | 
 is_admin             | boolean                 |           |          |                                  | plain    |              | 
 allow_git_hook       | boolean                 |           |          |                                  | plain    |              | 
 allow_import_local   | boolean                 |           |          |                                  | plain    |              | 
 prohibit_login       | boolean                 |           |          |                                  | plain    |              | 
 avatar               | character varying(2048) |           | not null |                                  | extended |              | 
 avatar_email         | character varying(255)  |           | not null |                                  | extended |              | 
 use_custom_avatar    | boolean                 |           |          |                                  | plain    |              | 
 num_followers        | integer                 |           |          |                                  | plain    |              | 
 num_following        | integer                 |           | not null | 0                                | plain    |              | 
 num_stars            | integer                 |           |          |                                  | plain    |              | 
 num_repos            | integer                 |           |          |                                  | plain    |              | 
 description          | character varying(255)  |           |          |                                  | extended |              | 
 num_teams            | integer                 |           |          |                                  | plain    |              | 
 num_members          | integer                 |           |          |                                  | plain    |              | 
Indexes:
    "user_pkey" PRIMARY KEY, btree (id)
    "UQE_user_lower_name" UNIQUE, btree (lower_name)
    "UQE_user_name" UNIQUE, btree (name)

当我尝试选择类似的列时

gogs=> select email from user;

我收到错误

ERROR:  column "email" does not exist
LINE 1: select email from user;

请注意,我也尝试过没有成功

select "email" from user;
select "Email" from user;
select "EMAIL" from user;

这真是令人费解。有人可以帮忙吗?

【问题讨论】:

    标签: sql postgresql


    【解决方案1】:

    user 中选择将返回您的用户名。为了从具有该名称的表中进行选择,您需要在其前面加上模式名称,在这种情况下它是公共的。所以你需要执行:

    SELECT * FROM public.user;
    

    【讨论】:

      猜你喜欢
      • 2015-03-30
      • 2023-01-19
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      • 2017-11-23
      • 2013-09-22
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多