DEV Community

PostgreSQL "relation does not exist" (42P01) when the table is right there: two causes

You created the table. SELECT says ERROR: relation "..." does not exist. If it isn't a typo, it's almost always one of these:

  • The table lives in another schema that isn't on your search_path.
  • The table was created with a quoted, mixed-case name and you're querying it unquoted.

I reproduced both on PostgreSQL 17.12 in a container and ran the check and the fix. Every output below is exactly what psql printed.

The table is in another schema

-- as postgres
CREATE SCHEMA sales AUTHORIZATION lab_a;
-- as lab_a
CREATE TABLE sales.orders (id int PRIMARY KEY, amount numeric);
INSERT INTO sales.orders VALUES (1, 100);

Query it without the schema:

SELECT * FROM orders;
ERROR: 42P01: relation "orders" does not exist
LINE 1: SELECT * FROM orders;
               ^

Check: which schema is it in?

SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders';
schemaname | tablename
------------+-----------
sales      | orders
(1 row)

And where is this session looking?

SHOW search_path;
search_path
-----------------
"$user", public
(1 row)

sales isn't there. Unqualified names are only looked up in the schemas on search_path, so the table "doesn't exist".

Fix: qualify it (sales.orders) or add the schema to the role's default path:

ALTER ROLE lab_a SET search_path = "$user", sales, public;
ALTER ROLE

After reconnecting:

id | amount
----+--------
1  | 100
(1 row)

ALTER ROLE ... SET applies to new connections. For the current session only, use SET search_path = ...;.

A quoted mixed-case table name

CREATE TABLE "Users" (id int PRIMARY KEY, email text);
INSERT INTO "Users" VALUES (1, 'a@example.com');
SELECT * FROM Users;
ERROR: 42P01: relation "users" does not exist
LINE 1: SELECT * FROM Users;
                      ^

Look at the message: "users", lowercase. Unquoted identifiers are folded to lowercase before lookup.

The real name:

SELECT table_name FROM information_schema.tables WHERE lower(table_name) = 'users';
table_name
------------
Users
(1 row)

So quote it:

SELECT * FROM "Users";
id | email
----+---------------
1  | a@example.com
(1 row)

ORMs and GUI tools often create quoted names, which is why this shows up so often. For new tables, lowercase names save you the trouble.

Takeaways

  • Name in the error is lowercased → suspect a quoted mixed-case table.
  • Name is right but "does not exist" → check pg_tables for the schema, then SHOW search_path.
  • Fix: qualify the schema, add it to search_path, or quote the name.

I keep a reference where each 42P01 cause is reproduced, checked and fixed on a real PostgreSQL, with the raw output: https://errorfixlab.96ssg.com/en/pg-42p01/

This post was written with the help of generative AI, based on output actually executed in a test environment.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.