155

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";

I will get a list of the columns returned properly.

However, when I query:

SELECT *
FROM "my_table";

I get the error:

(ProgrammingError) relation "my_table" does not exist
'SELECT *\n    FROM "my_table"\n' {}

Any thoughts on why I can get the columns, but can't query the table? Goal is to be able to query the table.

patkil
  • 1,929
  • 3
  • 16
  • 17
  • can you do the same with another table? try creating a new one. – Juan Carlos Oropeza Apr 20 '16 at 19:40
  • 2
    The query you have shown can't work. `WHERE table_name="my_table";` is invalid because `"my_table"` references a column name and there is no such column in `information_schema.columns`. Please [edit] your question and add the **exact** `create table` statement you used to create the table. –  Apr 20 '16 at 20:30
  • 1
    do check this one https://dba.stackexchange.com/questions/192897/postgres-relation-does-not-exist-error – Luv33preet Jul 26 '18 at 08:01

12 Answers12

143

You have to include the schema if isnt a public one

SELECT *
FROM <schema>."my_table"

Or you can change your default schema

SHOW search_path;
SET search_path TO my_schema;

Check your table schema here

SELECT *
FROM information_schema.columns

enter image description here

For example if a table is on the default schema public both this will works ok

SELECT * FROM parroquias_region
SELECT * FROM public.parroquias_region

But sectors need specify the schema

SELECT * FROM map_update.sectores_point
Juan Carlos Oropeza
  • 47,252
  • 12
  • 78
  • 118
  • 4
    Correct answer. If not familiar with the SQL standard’s hierarchy of Cluster > Catalog > Schema > Table, see the Question, [What's the difference between a catalog and a schema in a relational database?](http://stackoverflow.com/q/7022755/642706) and [my diagram](http://i.stack.imgur.com/FqyMq.png). – Basil Bourque Apr 20 '16 at 19:52
  • 1
    Yup - this did it, thanks much. Of course now I'm getting `permission denied`, but that at least I know where to go with. – patkil Apr 20 '16 at 20:33
  • 22
    For future readers of this thread, this error could also occur, like it did in my case, when the Schema name & Table name have mixed case characters & are not enclosed individually in double quotes. In other words they have to be specified as: "my_Schema"."my_Table" – Snidhi Sofpro Jul 30 '19 at 07:59
  • 3
    @SnidhiSofpro Well that also happen with field names, My suggestion just use lower case names so you don't have to add the hassle of double quotes. – Juan Carlos Oropeza Jul 30 '19 at 13:44
  • 2
    What if everything works with `SET search_path TO my_schema;` in postgres shell but the exact thing wont work in python code? I dont have any case sensitive problems. I'm sure! – Ghasem Dec 05 '19 at 09:40
  • @SnidhiSofpro comment was the solution for me. Key sentence from what they referenced "Quoting an identifier also makes it case-sensitive, whereas unquoted names are always folded to lower case" – Athir Nuaimi Jun 29 '21 at 05:05
61

You can try:

SELECT * 
FROM public."my_table"

Don't forget double quotes near my_table.

4b0
  • 21,981
  • 30
  • 95
  • 142
23

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" (
...
dfrankow
  • 20,191
  • 41
  • 152
  • 214
20

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.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • 1
    this is it for me. check you have set the correct database in your connection properties. that you can see the table does not mean it's correctly set. – Mario Codes Apr 22 '22 at 10:56
  • This fixed an error I have. I had mistyped a DB name in foreign data server configuration where I tried to find my table. – Vind Iskald Aug 20 '22 at 13:45
6

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

dmigwi
  • 61
  • 1
  • 5
3

The error can be caused by access restrictions. Solution:

GRANT ALL PRIVILEGES ON DATABASE my_database TO my_user;
Marcel
  • 2,810
  • 2
  • 26
  • 46
3

I was using pgAdmin to create my tables and while I was not using reserved words, the generated table had a quote in the name and a couple of columns had quotes in them. Here is an example of the generated SQL.

CREATE TABLE public."Test"
(
    id serial NOT NULL,
    data text NOT NULL,
    updater character varying(50) NOT NULL,
    "updateDt" time with time zone NOT NULL,
    CONSTRAINT test_pk PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE public."Test"
    OWNER to svc_newnews_app;

All of these quotes were inserted at "random". I just needed to drop and re-create the table again without the quotes.

Tested on pgAdmin 4.26

Chewy
  • 651
  • 6
  • 21
3

Please ensure that:

  1. Your password is non-empty
  2. In case it is empty, do not pass the password param in the connection string

This is one of the most common errors when starting out with the tutorial.

Kritika
  • 41
  • 2
2

Keep all your table names in lower case because when you rollback and then go to latest, it's looking for lowercase apparently.

Erick
  • 31
  • 1
  • 4
1

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

samsri
  • 1,104
  • 14
  • 25
0

Lets say we have database name as students and schema name as studentinformation then to use all the table of this schema we need to set the path first which we can do in postgresql like:

client.connect()
.then(()=>console.log("connected succesfully"))
.then(()=>client.query("set search_path to students"))
.then(()=>client.query("show search_path"))
.then(()=>client.query("set search_path to studentinformation"))
.then(()=>client.query("show search_path"))
.then(results => console.table(results.rows)) //setting the search path 
Toni
  • 1,555
  • 4
  • 15
  • 23
-1

I was using psql from PostgreSQL, and somehow I created the table in the "postgres=#" directory instead of first connecting to the database and creating it there.

So make sure that you connected to the database you want before creating tables