How to Fix PostgreSQL 'Relation Does Not Exist' Errors
'relation does not exist' in PostgreSQL, fixed for beginners on a real database: what the message means, how PostgreSQL finds a table (the database you're connected to, the schemas on your search path, lowercase folding of unquoted names), and five real causes with their fixes: the wrong database in your connection address, capital letters in double quotes, a table in another schema, a schema you have no USAGE on, and a table that hasn't been committed.

The error, before and after
Your app stops with this error: relation orders does not exist. But the table is right there, and four quick checks show where PostgreSQL looked.

Here's that app, a few lines of Python. It connects fine, then the query fails with that same error. So where did PostgreSQL look?
python3 app.pyTraceback (most recent call last): File "/home/dev/app/app.py", line 7, in <module> cur.execute("SELECT id, item, total FROM orders ORDER BY id") psycopg2.errors.UndefinedTable: relation "orders" does not exist LINE 1: SELECT id, item, total FROM orders ORDER BY id ^
UndefinedTable is Python's name for this error. Relation is PostgreSQL's word for a table. And the name in quotes is exactly what it searched for.

The quick fix: four checks
So here's the quick fix: four checks, in this order. Which database are you in? Is the name spelled exactly, capitals and all?

Is the table in a schema PostgreSQL searches, and are you allowed into it? A schema is a kind of folder. And was the table saved for everyone to see? We'll run all four on a real database. But first: how can PostgreSQL miss a table that's right there?

How PostgreSQL finds a table
Picture an office building. You're inside one building: your database. You check only the floors on your list, and only those your key card opens. The name on the door must match, letter for letter. Miss any of those: the room does not exist.
Here's the real thing. One PostgreSQL server holds several databases, and a connection sees only one of them. Inside a database, tables live in schemas, like folders. Every database has one called public. When you type a name like orders, PostgreSQL checks your search path. That's a list of schemas, checked in order. It skips any schema you aren't allowed into. The first match wins. No match: relation does not exist.
The wrong database
Question one: which database? Your app reads its address from a settings file called.env, and the address ends in /postgres. That last part is the database name. Every new server comes with a default database called postgres. That makes it easy to copy by mistake. Now open psql, the PostgreSQL terminal tool, with that same address. Ask for orders, and you get the same error.
cat .envDATABASE_URL=postgresql://app@db:5432/postgres
psql postgresql://app@db:5432/postgrespsql (19.0 (Ubuntu 19.0-2.pgdg24.04+1)) Type "help" for help.
postgres=> SELECT * FROM orders;ERROR: relation "orders" does not exist LINE 1: SELECT * FROM orders; ^
Now ask PostgreSQL directly: select current_database, and it answers: postgres. pg_tables is PostgreSQL's own list of every table in this database. Search it for orders: zero rows. List the server's databases, and there's the other one: shop.
postgres=> SELECT current_database();current_database ------------------ postgres (1 row)
postgres=> SELECT schemaname, tablename FROM pg_tables WHERE tablename ILIKE 'orders';schemaname | tablename ------------+----------- (0 rows)
postgres=> SELECT datname FROM pg_database WHERE NOT datistemplate;datname ---------- postgres shop (2 rows)
Backslash commands are psql shortcuts, not SQL. \c, short for connect, switches databases. The prompt now reads shop, and orders has four rows. One habit worth keeping: glance at the prompt to see which database you're in, before you blame the table.
postgres=> \c shopYou are now connected to database "shop" as user "app".
shop=> SELECT * FROM orders;id | item | total ----+--------------+------- 1 | coffee beans | 18.50 2 | mug | 12.00 3 | grinder | 89.00 4 | filters | 6.25 (4 rows)
Then fix the app itself. In.env, the end of the address changes from /postgres to /shop.

Run it again, and the app lists all four orders.
cat .envDATABASE_URL=postgresql://app@db:5432/shop
python3 app.py1 coffee beans 18.50 2 mug 12.00 3 grinder 89.00 4 filters 6.25
Capital letters
Question two: the exact name. Your shop also has a customers table. Like many tables, it was made by a tool, not typed by hand. \dt, psql's shortcut for listing tables, shows it: Customers, capital C. So you type it exactly like that. Guess first: does it work? No. And the error names it in lowercase: customers. Not what you typed.
shop=> \dtList of tables Schema | Name | Type | Owner --------+-----------+-------+------- public | Customers | table | app public | orders | table | app (2 rows)
shop=> SELECT * FROM Customers;ERROR: relation "customers" does not exist LINE 1: SELECT * FROM Customers; ^
Here's why. PostgreSQL lowercases every name you don't put in double quotes, before it searches. Your capital C never arrives. But the tool did use quotes when it made the table, and a quoted name keeps its capitals. So the table really is called Customers, capital C, and an unquoted name can never match it.

Search pg_tables with ILIKE, which ignores capitals, and it's there: public, Customers. Fix one: use double quotes, exactly as it was made. Three rows. Fix two: rename it to lowercase, once. Only do that if nothing else still uses the old name. Now even a capital C with no quotes works: PostgreSQL lowercases it, and finds customers, small c.
shop=> SELECT schemaname, tablename FROM pg_tables WHERE tablename ILIKE 'customers';schemaname | tablename ------------+----------- public | Customers (1 row)
shop=> SELECT * FROM "Customers";id | email ----+--------------- 1 | a@example.com 2 | b@example.com 3 | c@example.com (3 rows)
shop=> ALTER TABLE "Customers" RENAME TO customers;ALTER TABLE
shop=> SELECT * FROM Customers;id | email ----+--------------- 1 | a@example.com 2 | b@example.com 3 | c@example.com (3 rows)
Another schema
Question three: which schemas? Your shop keeps its invoices in a schema of their own, called billing. Ask for invoices: does not exist. SHOW search_path lists where PostgreSQL looks. First comes a placeholder, $user. It means a schema with the same name as your login, if one exists. Then public. Billing isn't on the list.
shop=> SELECT * FROM invoices;ERROR: relation "invoices" does not exist LINE 1: SELECT * FROM invoices; ^
shop=> SHOW search_path;search_path ----------------- "$user", public (1 row)
On the map, billing is right next door. We just have to tell PostgreSQL to look there.
\dn lists this database's schemas: billing and public. And the search finds invoices inside billing. Fix one: name the schema in the query. billing.invoices, and the three invoices come back.
shop=> \dnList of schemas Name | Owner ---------+------------------- billing | app public | pg_database_owner (2 rows)
shop=> SELECT schemaname, tablename FROM pg_tables WHERE tablename ILIKE 'invoices';schemaname | tablename ------------+----------- billing | invoices (1 row)
shop=> SELECT * FROM billing.invoices;id | order_id | amount ----+----------+-------- 1 | 1 | 18.50 2 | 2 | 12.00 3 | 3 | 89.00 (3 rows)
Fix two: put billing on the search path for good. A role is PostgreSQL's word for a login. ALTER ROLE saves this search path for your login, app. But only new connections pick it up, so this one still shows the old path. Reconnect, and the path is public, then billing. A plain invoices works.
shop=> ALTER ROLE app SET search_path = public, billing;ALTER ROLE
shop=> SHOW search_path;search_path ----------------- "$user", public (1 row)
shop=> \c shopYou are now connected to database "shop" as user "app".
shop=> SHOW search_path;search_path ----------------- public, billing (1 row)
shop=> SELECT * FROM invoices;id | order_id | amount ----+----------+-------- 1 | 1 | 18.50 2 | 2 | 12.00 3 | 3 | 89.00 (3 rows)
A schema you can't use
Question three has a second half: being allowed in. Your reporting tool logs in as a second user, called reporter. Its search path already starts with billing, right where the invoices are. This terminal is logged in as reporter. Keep a second one open, logged in as app, the user who owns billing. Guess first: what does reporter get for invoices? Relation does not exist. Again.
shop=> SHOW search_path;search_path ----------------- billing, public (1 row)
shop=> SELECT * FROM invoices;ERROR: relation "invoices" does not exist LINE 1: SELECT * FROM invoices; ^
Why? Your key card doesn't open that floor. PostgreSQL silently skips any schema on the path that you aren't allowed to use.
Name the schema, and the real reason finally shows: permission denied for schema billing. A built-in check, has_schema_privilege, agrees. It answers f, for false. Reporter just isn't allowed into billing.
shop=> SELECT * FROM billing.invoices;ERROR: permission denied for schema billing LINE 1: SELECT * FROM billing.invoices; ^
shop=> SELECT has_schema_privilege('billing', 'USAGE');has_schema_privilege ---------------------- f (1 row)
Switch to the other window, the one logged in as app, which owns billing. From there, grant reporter usage on the schema. Now reporter's error changes: permission denied for the table. Usage only lets you into the schema. Reading a table needs select. Grant it, and the three invoices come back.
shop=> GRANT USAGE ON SCHEMA billing TO reporter;GRANT
shop=> SELECT * FROM invoices;ERROR: permission denied for table invoices
shop=> GRANT SELECT ON ALL TABLES IN SCHEMA billing TO reporter;GRANT
shop=> SELECT * FROM invoices;id | order_id | amount ----+----------+-------- 1 | 1 | 18.50 2 | 2 | 12.00 3 | 3 | 89.00 (3 rows)
Not committed yet
Question four, the last one: was the table saved for everyone to see? Here's a table that exists for only one connection. In this window, a migration is running: a script that changes your tables. It starts with BEGIN. That opens a transaction, a group of changes no one else sees until they're saved.
shop=> BEGIN;BEGIN
Now it creates a refunds table and adds a row. See the star in the prompt? That means the transaction is still open. In here, refunds works.
shop=*> CREATE TABLE refunds (order_id INT, amount NUMERIC(8,2));CREATE TABLE
shop=*> INSERT INTO refunds VALUES (2, 12.00);INSERT 0 1
shop=*> SELECT * FROM refunds;order_id | amount ----------+-------- 2 | 12.00 (1 row)
From any other connection, like your app's, refunds does not exist, and pg_tables has no refunds at all. Until COMMIT, a new table is visible only inside its own transaction. Commit, and the other connection sees it straight away.
shop=> SELECT * FROM refunds;ERROR: relation "refunds" does not exist LINE 1: SELECT * FROM refunds; ^
shop=> SELECT schemaname, tablename FROM pg_tables WHERE tablename ILIKE 'refunds';schemaname | tablename ------------+----------- (0 rows)
shop=*> COMMIT;COMMIT
shop=> SELECT * FROM refunds;order_id | amount ----------+-------- 2 | 12.00 (1 row)
Stop it coming back
To stop it coming back: end every connection address with the right database name. Name tables in lowercase, with no quotes. Put the schema name in your queries, and grant usage along with select. And make sure every migration commits.

Recap
So relation does not exist means PostgreSQL looked somewhere your table isn't. Find where it looked, and you've found your table.
If this found your missing table, subscribe for more fixes like it. And a like helps YouTube show this to more beginners.
The demo files and every command are linked in the description, so you can break and fix all five yourself. Thanks for watching Cloud Explainers.
Sources
Demos: two containers on our own Mac standing in for your database server and your laptop. The shop and its tables are made up; every command, output and error was real. Watch the full video on YouTube.