PostgreSQL 17.12Lab-verified · 2026-12-01

PostgreSQL error 42P01: relation does not exist

PostgreSQL could not find a table or view with that name in the schemas it searches.

PostgreSQL 17.12
42P01: relation "customer" does not exist

Jump to a cause

  1. 01The table name is misspelled or the table does not exist
  2. 02The table was created with quotes and uppercase letters
  3. 03The table is in a schema that is not in search_path
  4. 04The table is in another database (shop), but you connected to the default one
  5. 05Unqualified table name inside a function: works for the owner, fails for another user

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
Setuprun as lab_a
CREATE TABLE customers (id int PRIMARY KEY, name text);
INSERT INTO customers VALUES (1, 'Kim'), (2, 'Lee');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT count(*) FROM customer;
✕ ERROR
ERROR:  42P01: relation "customer" does not exist
LINE 1: SELECT count(*) FROM customer;
                             ^
Checkrun as lab_a
SELECT table_schema, table_name FROM information_schema.tables WHERE table_name LIKE 'customer%';
OUTPUT
 table_schema | table_name
--------------+------------
 lab_a        | customers
(1 row)
After the fixrun as lab_a
SELECT count(*) FROM customers;
✓ SUCCESS
 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
Setuprun as lab_a
CREATE TABLE "Users" (id int PRIMARY KEY, email text);
INSERT INTO "Users" VALUES (1, 'a@example.com');
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
SELECT * FROM Users;
✕ ERROR
ERROR:  42P01: relation "users" does not exist
LINE 1: SELECT * FROM Users;
                      ^
Checkrun as lab_a
SELECT table_name FROM information_schema.tables WHERE lower(table_name) = 'users';
OUTPUT
 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.

After the fixrun as lab_a
SELECT * FROM "Users";
✓ SUCCESS
 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
Setuprun as postgres
CREATE SCHEMA sales AUTHORIZATION lab_a;
OUTPUT
CREATE SCHEMA
Setuprun as lab_a
CREATE TABLE sales.orders (id int PRIMARY KEY, amount numeric);
INSERT INTO sales.orders VALUES (1, 100);
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
SELECT * FROM orders;
✕ ERROR
ERROR:  42P01: relation "orders" does not exist
LINE 1: SELECT * FROM orders;
                      ^
Check the causerun as lab_a
SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders';
OUTPUT
 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.

Checkrun as lab_a
SHOW search_path;
OUTPUT
   search_path
-----------------
 "$user", public
(1 row)
Fixrun as lab_a
ALTER ROLE lab_a SET search_path = "$user", sales, public;
OUTPUT
ALTER ROLE

ALTER ROLE … SET search_path applies from the next login. For a single query, write the schema explicitly: SELECT * FROM sales.orders;.

After the fixrun as lab_a
SELECT * FROM orders;
✓ SUCCESS
 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
Setuprun as postgres
CREATE DATABASE shop OWNER lab_a;
OUTPUT
CREATE DATABASE
Setuprun as lab_a
\c shop
CREATE TABLE public.orders (id int PRIMARY KEY, amount numeric);
INSERT INTO public.orders VALUES (1, 100);
OUTPUT
You are now connected to database "shop" as user "lab_a".
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
SELECT * FROM orders;
✕ ERROR
ERROR:  42P01: relation "orders" does not exist
LINE 1: SELECT * FROM orders;
                      ^
Check the causerun as lab_a
SELECT current_database(), current_schema();
SELECT datname FROM pg_database WHERE NOT datistemplate ORDER BY datname;
OUTPUT
 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.

Checkrun as lab_a
\c shop
SELECT current_database();
OUTPUT
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).

After the fixrun as lab_a
\c shop
SELECT * FROM orders;
✓ SUCCESS
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
Setuprun as lab_a
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();
OUTPUT
CREATE TABLE
INSERT 0 2
CREATE FUNCTION
GRANT
GRANT
GRANT
 order_total
-------------
         350
(1 row)
Reproducerun as lab_b
SELECT lab_a.order_total();
✕ ERROR
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 inlining
Check the causerun as lab_b
SHOW search_path;
SELECT current_user, current_schemas(false);
OUTPUT
   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.

Fixrun as lab_a
ALTER FUNCTION order_total() SET search_path = lab_a;
OUTPUT
ALTER FUNCTION

Pinning 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.

After the fixrun as lab_b
SELECT lab_a.order_total();
✓ SUCCESS
 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