Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-12-01 · container image postgres:17
42P01 does not always mean the table is missing. PostgreSQL looks the name up after folding unquoted identifiers to lowercase, and only in the schemas listed in search_path. If the table exists under a different spelling, case or schema, you get the same error.
The message shows the name PostgreSQL actually looked for, already lowercased. Compare it with what is really in the database (information_schema.tables) before changing anything.
01The table name is misspelled or the table does not exist
The most common case. The table is customers, but the query says customer. Search the catalog for part of the name to see what actually exists in your schemas.
Setup
CREATE TABLE customers (id int PRIMARY KEY, name text);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');CREATE TABLE
INSERT 0 2SELECT count(*) FROM customer;ERROR: 42P01: relation "customer" does not exist
LINE 1: SELECT count(*) FROM customer;
^SELECT table_schema, table_name FROM information_schema.tables WHERE table_name LIKE 'customer%'; table_schema | table_name
--------------+------------
lab_a | customers
(1 row)SELECT count(*) FROM customers; count
-------
2
(1 row)02The table was created with quotes and uppercase letters
A GUI tool, ORM or migration created the table as "Users". Quoted identifiers keep their case, but SELECT * FROM Users is folded to users, so PostgreSQL cannot find it. The error message shows the lowercased name users.
Setup
CREATE TABLE "Users" (id int PRIMARY KEY, email text);
INSERT INTO "Users" VALUES (1, 'a@example.com');CREATE TABLE
INSERT 0 1SELECT * FROM Users;ERROR: 42P01: relation "users" does not exist
LINE 1: SELECT * FROM Users;
^SELECT table_name FROM information_schema.tables WHERE lower(table_name) = 'users'; table_name
------------
Users
(1 row)Either always quote the name ("Users") or rename the table once to lowercase with ALTER TABLE "Users" RENAME TO users; so that unquoted queries work.
SELECT * FROM "Users"; id | email
----+---------------
1 | a@example.com
(1 row)03The table is in a schema that is not in search_path
The table is sales.orders, but the session only searches "$user", public. An unqualified orders is never looked up in sales.
Setup
CREATE SCHEMA sales AUTHORIZATION lab_a;CREATE SCHEMACREATE TABLE sales.orders (id int PRIMARY KEY, amount numeric);
INSERT INTO sales.orders VALUES (1, 100);CREATE TABLE
INSERT 0 1SELECT * FROM orders;ERROR: 42P01: relation "orders" does not exist
LINE 1: SELECT * FROM orders;
^SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders'; schemaname | tablename
------------+-----------
sales | orders
(1 row)The table does exist, but in schema sales. An unqualified orders is looked up only in the schemas on search_path, and sales is not one of them.
SHOW search_path; search_path
-----------------
"$user", public
(1 row)ALTER ROLE lab_a SET search_path = "$user", sales, public;ALTER ROLEALTER ROLE … SET search_path applies from the next login. For a single query, write the schema explicitly: SELECT * FROM sales.orders;.
SELECT * FROM orders; id | amount
----+--------
1 | 100
(1 row)04The table is in another database (shop), but you connected to the default one
orders was created in the shop database, but the session connected to the user's default database lab. PostgreSQL only sees the database you are connected to, so even with the right schema and search_path it cannot find a table in another database.
Setup
CREATE DATABASE shop OWNER lab_a;CREATE DATABASE\c shop
CREATE TABLE public.orders (id int PRIMARY KEY, amount numeric);
INSERT INTO public.orders VALUES (1, 100);You are now connected to database "shop" as user "lab_a".
CREATE TABLE
INSERT 0 1SELECT * FROM orders;ERROR: 42P01: relation "orders" does not exist
LINE 1: SELECT * FROM orders;
^SELECT current_database(), current_schema();
SELECT datname FROM pg_database WHERE NOT datistemplate ORDER BY datname; current_database | current_schema
------------------+----------------
lab | lab_a
(1 row)
datname
----------
lab
postgres
shop
(3 rows)current_database is lab, and shop is a separate entry in the database list. The table lives in a different database from the one you are connected to.
\c shop
SELECT current_database();You are now connected to database "shop" as user "lab_a".
current_database
------------------
shop
(1 row)After \c shop, the same SELECT * FROM orders; worked. In an application, check the database name in the connection string (dbname, or /shop in the URL).
\c shop
SELECT * FROM orders;You are now connected to database "shop" as user "lab_a".
id | amount
----+--------
1 | 100
(1 row)05Unqualified table name inside a function: works for the owner, fails for another user
When lab_a created order_total(), SELECT order_total(); returned 350. When lab_b called lab_a.order_total(), the function body could not find orders. The CONTEXT line SQL function "order_total" shows the error came from inside the function.
Unqualified names inside a function are resolved with the caller's search_path. lab_b's search_path only covers lab_b and public, not the lab_a schema where orders lives.
Setup
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 100), (2, 250);
CREATE FUNCTION order_total() RETURNS numeric
LANGUAGE sql
AS $$ SELECT sum(amount) FROM orders $$;
GRANT USAGE ON SCHEMA lab_a TO lab_b;
GRANT SELECT ON orders TO lab_b;
GRANT EXECUTE ON FUNCTION order_total() TO lab_b;
SELECT order_total();CREATE TABLE
INSERT 0 2
CREATE FUNCTION
GRANT
GRANT
GRANT
order_total
-------------
350
(1 row)SELECT lab_a.order_total();ERROR: 42P01: relation "orders" does not exist
LINE 1: SELECT sum(amount) FROM orders
^
QUERY: SELECT sum(amount) FROM orders
CONTEXT: SQL function "order_total" during inliningSHOW search_path;
SELECT current_user, current_schemas(false); search_path
-----------------
"$user", public
(1 row)
current_user | current_schemas
--------------+-----------------
lab_b | {lab_b,public}
(1 row)lab_b's current_schemas is {lab_b,public}. lab_a, which holds the orders table the function uses, is not in the list.
ALTER FUNCTION order_total() SET search_path = lab_a;ALTER FUNCTIONPinning the function's search_path with ALTER FUNCTION order_total() SET search_path = lab_a; made lab_b's call return 350. Writing lab_a.orders inside the function also works.
SELECT lab_a.order_total(); order_total
-------------
350
(1 row)FAQ
How do I fix "relation does not exist" in PostgreSQL?
Check the exact name PostgreSQL looked for in the message, then search information_schema.tables for it. Fix the spelling, quote a mixed-case name ("Users"), or add the schema to search_path or to the query (sales.orders).
Why does PostgreSQL say relation does not exist when the table exists?
Usually because of case or schema. A table created as "Users" is not found by Users, and a table in another schema is not found unless that schema is in search_path.
What does SQLSTATE 42P01 mean?
42P01 is undefined_table: the statement refers to a table, view, sequence or other relation that PostgreSQL cannot find with the current name and search path.
How do I see which schemas PostgreSQL searches?
Run SHOW search_path;. The default is "$user", public, meaning a schema with your role name first, then public.
It works in psql but my app says relation does not exist. Why?
Check which database the app is connected to. If SELECT current_database(); is not the database where the table was created, it cannot be found. In the run above, a session on lab looked for a table in shop and got 42P01.
My function fails with relation does not exist when another user runs it.
Unqualified table names in a function are looked up with the caller's search_path. Pin it with ALTER FUNCTION ... SET search_path = schema; or schema-qualify the names in the body.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12