FluentDBFluentDB
PostgreSQL errors

PostgreSQL: column reference is ambiguous

ERRORSQLSTATE 42702ambiguous_column
ERROR: column reference "<name>" is ambiguous

The column name you used exists in more than one table in the query, so Postgres cannot tell which you mean. Qualify it with a table name or alias.

What this error means

When a query joins two tables that both have a column with the same name (id is the classic case), an unqualified reference is ambiguous, and Postgres refuses to guess which one you meant.

It also happens inside PL/pgSQL when a variable shares a name with a table column.

How to fix it

01

Two joined tables share the column name

Prefix the column with its table alias.

sql
SELECT o.id, c.id AS customer_id
FROM orders o
JOIN customers c ON c.id = o.customer_id;
02

A PL/pgSQL variable clashes with a column

Rename the variable, or tell Postgres to prefer the column.

text
#variable_conflict use_column

How to avoid it next time

  • Alias every table and qualify columns in any query that joins more than one table.
  • Give PL/pgSQL variables names that cannot collide with column names.

Frequently asked questions

Why does 'id' cause this?

Both joined tables have an id column, so id alone is ambiguous. Write o.id or c.id.

How do I fix it in a function?

Rename the PL/pgSQL variable so it does not match a column, or add #variable_conflict use_column to the function body.

Stop struggling with SQL

Use FluentDB, the AI database client for macOS.