Fix "column does not exist" in PostgreSQL
ERROR: column "discounted" does not exist
Postgres looked through every table in your FROM clause and found no column with that name. The name may be misspelled, it may not exist yet at that point in the query, or Postgres may be reading something as a column name that you meant as a value.
Why it happens
- A typo or a guessed column name, like signup instead of signup_date.
- Using an alias from SELECT (AS discounted) in WHERE. WHERE runs before SELECT, so the alias doesn't exist yet.
- Writing a text value in double quotes, like "Canada". In Postgres, double quotes mean a column or table name.
- A column created with capital letters in double quotes ("Email") must always be written with those quotes.
How to fix it
1. Fix the column name
Fails
SELECT c.name, c.signup FROM customers c WHERE c.country = 'Canada';
Works
SELECT c.name, c.signup_date FROM customers c WHERE c.country = 'Canada';
When the column has a table prefix, the error reads column c.signup does not exist, without quotes. Check the real column names in the schema panel instead of guessing.
2. Don't use a SELECT alias in WHERE
Fails
SELECT id, amount, amount * 0.9 AS discounted FROM orders WHERE discounted > 100;
Works
SELECT id, amount, amount * 0.9 AS discounted FROM orders WHERE amount * 0.9 > 100;
Repeat the expression in WHERE, or wrap the query in a subquery or CTE and filter on the alias outside. ORDER BY is the one clause that can use SELECT aliases.
3. Put text values in single quotes
Fails
SELECT name, email FROM customers WHERE country = "Canada";
Works
SELECT name, email FROM customers WHERE country = 'Canada';
Single quotes are for values ('Canada'), double quotes are for names ("customers"). That's why Postgres complains about a column called Canada.
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.