Postgresql tables exists, but getting “relation does not exist” when querying

后端 未结 8 2277
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 23:22

I have a postgresql db with a number of tables. If I query:

SELECT column_name
FROM information_schema.columns
WHERE table_name=\"my_table\";
8条回答
  •  攒了一身酷
    2020-11-30 23:52

    I had to include double quotes with the table name.

    db=> \d
                               List of relations
     Schema |                     Name                      | Type  | Owner 
    --------+-----------------------------------------------+-------+-------
     public | COMMONDATA_NWCG_AGENCIES                      | table | dan
     ...
    
    db=> \d COMMONDATA_NWCG_AGENCIES
    Did not find any relation named "COMMONDATA_NWCG_AGENCIES".
    

    ???

    Double quotes:

    db=> \d "COMMONDATA_NWCG_AGENCIES"
                             Table "public.COMMONDATA_NWCG_AGENCIES"
              Column          |            Type             | Collation | Nullable | Default 
    --------------------------+-----------------------------+-----------+----------+---------
     ID                       | integer                     |           | not null | 
     ...
    

    Lots and lots of double quotes:

    db=> select ID from COMMONDATA_NWCG_AGENCIES limit 1;
    ERROR:  relation "commondata_nwcg_agencies" does not exist
    LINE 1: select ID from COMMONDATA_NWCG_AGENCIES limit 1;
                           ^
    db=> select ID from "COMMONDATA_NWCG_AGENCIES" limit 1;
    ERROR:  column "id" does not exist
    LINE 1: select ID from "COMMONDATA_NWCG_AGENCIES" limit 1;
                   ^
    db=> select "ID" from "COMMONDATA_NWCG_AGENCIES" limit 1;
     ID 
    ----
      1
    (1 row)
    

    This is postgres 11. The CREATE TABLE statements from this dump had double quotes as well:

    DROP TABLE IF EXISTS "COMMONDATA_NWCG_AGENCIES";
    
    CREATE TABLE "COMMONDATA_NWCG_AGENCIES" (
    ...
    

提交回复
热议问题