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

后端 未结 8 2195
没有蜡笔的小新
没有蜡笔的小新 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:47

    You can try:

    SELECT * 
    FROM public."my_table"
    

    Don't forget double quotes near my_table.

    0 讨论(0)
  • 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" (
    ...
    
    0 讨论(0)
  • 2020-11-30 23:55

    I had the same problem that occurred after I restored data from a postgres dumped db.

    My dump file had the command below from where things started going south.

        SELECT pg_catalog.set_config('search_path', '', false);
    

    Solutions:

    1. Probably remove it or change that false to be true.
    2. Create a private schema that will be used to access all the tables.

    The command above simply deactivates all the publicly accessible schemas.

    Check more on the documentation here: https://www.postgresql.org/docs/9.3/ecpg-connect.html

    0 讨论(0)
  • 2020-11-30 23:57

    I hit this error and it turned out my connection string was pointing to another database, obviously the table didn't exist there.

    I spent a few hours on this and no one else has mentioned to double check your connection string.

    0 讨论(0)
  • 2020-11-30 23:58

    The error can be caused by access restrictions. Solution:

    GRANT ALL PRIVILEGES ON DATABASE my_database TO my_user;
    
    0 讨论(0)
  • 2020-11-30 23:58

    In my case, the dump file I restored had these commands.

    CREATE SCHEMA employees;
    SET search_path = employees, pg_catalog;
    

    I've commented those and restored again. The issue got resolved

    0 讨论(0)
提交回复
热议问题