Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.

Introduction & Quick Summary
A query that reads perfectly fails on the server:
ERROR: column "userid" does not exist
LINE 1: SELECT userid FROM users;
^
HINT: Perhaps you meant to reference the column "users.user_id".Quick answer: the identifier you wrote is not the identifier that exists. Check the real column name, including its case.
\d users
-- user_id | bigint | not null
SELECT user_id FROM users;PostgreSQL folds every unquoted identifier to lower case. Write userId without quotes and the server looks for userid; create a column as "userId" with quotes and it keeps the mixed case, but then every reference must repeat the quotes exactly. Since almost no schema creates mixed-case columns on purpose, this error usually means either a typo, an identifier written with the wrong case, or a reference to a column that a migration has not created yet. The HINT line is your best tool: PostgreSQL computes the closest existing name and prints it.
Core Mechanics, Signatures & Reproducible Setup
Reproduce the case-folding trap in two statements:
CREATE TABLE demo_users ("userId" bigint);
SELECT userId FROM demo_users;
-- ERROR: column "userid" does not exist
SELECT "userId" FROM demo_users;
-- worksAnd the ordinary typo version, which is far more common:
CREATE TABLE users (user_id bigint, email text);
SELECT userid FROM users; -- ERROR: column "userid" does not exist
SELECT user_id FROM users; -- worksThe error code is 42703 (undefined_column). The sibling codes point at different objects and different fixes:
| Code | Message | Object missing | Check |
|---|---|---|---|
42703 | column "x" does not exist | column | \d table |
42P01 | relation "x" does not exist | table or view | \dt, schema path |
42883 | operator does not exist: text = integer | operator or cast | column types |
42702 | column reference "id" is ambiguous | duplicate name in a join | qualify with a table |
42601 | syntax error at or near "x" | parse error | quoting and keywords |
3F000 | schema "x" does not exist | schema | \dn |
Key Comparison Table
How an identifier is written decides how it is stored, which decides whether it is found:
| Written | Stored as | Found by | Found by |
|---|---|---|---|
userId | userid | userId | "userid" |
"userId" | userId | "userId" | not by userId |
"USER_ID" | USER_ID | "USER_ID" | not by user_id |
user_id | user_id | user_id | "user_id" |
Where to look when you need the real name, fastest first:
| Tool | Command | Output |
|---|---|---|
| psql describe | \d+ users | columns, types, defaults, indexes |
| psql listing | \d users | compact columns |
| catalog query | SELECT column_name FROM information_schema.columns WHERE table_name = 'users' | all visible schemas |
| catalog query | SELECT attname FROM pg_attribute WHERE attrelid = 'users'::regclass AND attnum > 0 | exactly one relation |
| editor introspection | expand the table in the sidebar | live schema |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. The hint is usually right
SELECT userid FROM users;
-- HINT: Perhaps you meant to reference the column "users.user_id".PostgreSQL compares the unknown identifier with nearby ones using edit distance. When the hint names a table-qualified column, it is telling you both the correct name and that the column exists on that relation. Follow it rather than guessing, and prefer \d+ table over a SELECT * to see the real shape. SELECT * also hides the problem until the application reads a field name that no longer matches.
2. Aliases, subqueries and where a column name is visible
SELECT u.id, u.email
FROM users u
WHERE email = 'a@example.com'; -- ambiguous if another joined table has email
SELECT u.id, COUNT(o.id) AS orders
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.id
HAVING orders > 3; -- works: HAVING sees the aliasWHERE cannot reference an output alias, because it is evaluated before the select list is computed. ORDER BY and GROUP BY can, and HAVING can in PostgreSQL. Writing WHERE orders > 3 produces 42703, and the HINT suggests nothing useful because the alias is not a column of any relation.
3. Search path and several schemas
SHOW search_path; -- "$user", public
SET search_path TO app, public;
SELECT * FROM app.users;Two schemas with a users table, and only the first on the search path is visible; a query that worked in one connection fails in another whose search_path differs. This is the classic difference between a migration tool (which often sets search_path explicitly) and an application connection (which inherits the role's default). Set it once per role (ALTER ROLE app_user SET search_path = app, public;) instead of relying on session defaults.
4. Migration drift: the column exists in staging, not in production
SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 5;
SELECT EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'locale'
) AS has_locale;When a deploy applies application code before its migration, every request touching the new column fails with 42703 for the length of the window. Ordering matters: run the migration first, or make the code tolerate the column's absence during rollout. A SELECT * inside a view makes this worse — a view's column list is frozen at creation time, so CREATE OR REPLACE VIEW is required after a rename, otherwise selecting the view reports column "x" does not exist even though the base table is fine.
5. JSON, JSONB and generated columns
SELECT payload->>'userid' FROM events; -- key lookup, no error if absent
SELECT (payload->>'user_id')::bigint AS uid FROM events;
SELECT payload->>'user_id' AS user_id FROM events;
-- ERROR: column "user_id" does not exist (when the arrow is missing)Forgetting an arrow turns a key name into a column reference: payload->'user_id' is a JSON member, payload->>'user_id' is text, but payload.user_id is a column of the table payload and fails with 42703 unless such a column exists. The error message then names user_id, which sends people looking at the table definition instead of at the expression. Casting text to the right type before comparing avoids the companion error operator does not exist: text = integer.
6. ORMs that quote identifiers by default
Most ORMs quote every identifier, which preserves case and makes the mapping explicit:
| Tool | Behaviour | Where the mismatch appears |
|---|---|---|
| Hibernate/JPA | quotes only when the name is escaped, otherwise uses PhysicalNamingStrategy | @Column(name = "userId") with a snake_case strategy |
| Prisma | maps model fields to columns with @map | schema drift between prisma db pull and hand edits |
| Sequelize | uses the field name as-is unless field: is set | JavaScript camelCase against snake_case columns |
| Django | column names come from the model field name | a manual db_column change |
| SQLAlchemy | Column("user_id") is the exact name | foreign_keys strings pointing at removed names |
Log the generated SQL before altering the schema. A statement that contains "userId" tells you immediately that the ORM is quoting a camelCase identifier and the database has user_id. Paste the failing statement into the SQL formatter to see the quoting clearly, and render the target schema with the SQL schema tool to compare names side by side.
Common Compilation/Runtime Mistakes & Version Gotchas
"Fixing" it by adding double quotes. SELECT "userid" still fails when the column is user_id, and now the query is case-sensitive forever. Quotes are for names that genuinely need them.
Using double quotes for string literals. WHERE status = "active" makes PostgreSQL look for a column named active and report 42703. String literals use single quotes.
Assuming the error is about the table. If the message names a column that exists in a table you did not join, the query is probably missing a join or uses the wrong alias.
Ignoring reserved words. SELECT "order" FROM orders works; SELECT order FROM orders is a syntax error, and SELECT "Order" FROM orders is 42703 because the quoted name keeps its capital O.
Version notes. PostgreSQL 9.5+ added IF NOT EXISTS to ALTER TABLE ... ADD COLUMN, which makes idempotent migrations easier and reduces drift. PostgreSQL 12 changed how pg_attribute reports dropped columns, so catalog queries that count attnum can shift after a DROP COLUMN. PostgreSQL 15 added MERGE, and merged statements often fail with 42703 in the WHEN NOT MATCHED THEN INSERT clause when a source column is referenced without its source alias. In all versions, prepared statements keep their parsed plan per connection, so a column added later is still unknown to a statement prepared before the change — reconnect or use DISCARD PLANS after a schema change.
The five-second check. Before editing a query, run \d table_name. One command, and the answer to 42703 is on the screen.
Reference material: PostgreSQL error codes and Lexical structure and identifiers.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
SQL Window Functions: ROW_NUMBER, RANK, DENSE_RANK, and LEAD/LAG Explained
How the four ranking and offset functions differ on ties, why the default RANGE frame makes running totals repeat, and how LAG builds gap-and-island.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
pg_dump: server version mismatch, aborting because of server version
pg_dump refuses to read a newer server. Install a matching client, run it from a container or the server host, and match pg_restore too.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.