Fix "relation does not exist" in PostgreSQL
ERROR: relation "customer" does not exist
Relation is Postgres's word for a table (or a view). The table name in your FROM or JOIN doesn't match any table Postgres can see right now. Either the name is off, it lives in another schema, or it was never created in this database.
Why it happens
- A typo, or singular vs plural: customer instead of customers.
- The table lives in a different schema, or you added a schema prefix that doesn't exist.
- The table was never created, or it was created in another database or in a transaction that was rolled back.
- The table was created with capitals in double quotes ("Orders"), so plain orders doesn't find it.
How to fix it
1. Fix the table name
Fails
SELECT name, email FROM customer WHERE country = 'Canada';
Works
SELECT name, email FROM customers WHERE country = 'Canada';
Table names must match exactly. Many schemas use plural names (customers, orders), so a missing s is the most common cause.
2. Use the right schema
Fails
SELECT id, amount FROM sales.orders WHERE status = 'completed';
Works
SELECT id, amount FROM public.orders WHERE status = 'completed';
Tables without a prefix are looked up in your search_path, which is usually just public. To see what exists and where, run: SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema = 'public';
3. Create the table before you query it
Fails
SELECT customer_id, total FROM big_spenders ORDER BY total DESC;
Works
CREATE TEMP TABLE big_spenders AS SELECT customer_id, SUM(amount) AS total FROM orders GROUP BY customer_id HAVING SUM(amount) > 300; SELECT customer_id, total FROM big_spenders ORDER BY total DESC;
If the table is just a step in a bigger query, a CTE (WITH big_spenders AS (...) SELECT ...) is simpler. A CTE only exists inside the one statement that defines it, so a second statement can't see it.
Try it yourself
This is the broken query, running on real PostgreSQL in your browser. Press Run to see the error explained, then fix it.
Starting Postgres in your browser…
Real Postgres in your browser. Edit the query and run it, or open it in the full playground.
Practice so it doesn't happen in an interview
- Your first querySELECT * · FROM
- Days to first purchaseCTE · MIN · date math
- Orders with customer namesINNER JOIN