PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 42P09: table reference is ambiguous

Two tables with the same name (from different schemas) are in one query, and a column is qualified by the bare table name.

PostgreSQL 17.12
42P09: table reference "orders" is ambiguous

Jump to a cause

  1. 01JOIN of two same-name tables, columns qualified by the table name
  2. 02UPDATE ... FROM with a same-name table
  3. 03DELETE ... USING with a same-name table

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17

In a query, a table without an alias is referred to by its name without the schema. If orders (your schema) and archive.orders are both in the FROM list, orders.id could mean either one, and PostgreSQL stops with 42P09 (ambiguous_alias).

This happens a lot when comparing or syncing with an archive, backup or staging schema. In all three cases below, giving each table its own alias fixed it.

01JOIN of two same-name tables, columns qualified by the table name

SELECT orders.id, orders.amount FROM orders JOIN archive.orders USING (id) failed with 42P09 pointing at the first orders.id.

With aliases o and a, the query returned current and archived amounts side by side (id 2: 200 vs 250).

Setup
Setuprun as postgres
DROP SCHEMA IF EXISTS archive CASCADE;
CREATE SCHEMA archive AUTHORIZATION lab_a;
OUTPUT
NOTICE:  00000: schema "archive" does not exist, skipping
DROP SCHEMA
CREATE SCHEMA
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO orders VALUES (1, 100, 'OPEN'), (2, 200, 'OPEN'), (3, 300, 'OPEN');
CREATE TABLE archive.orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO archive.orders VALUES (1, 100, 'CLOSED'), (2, 250, 'CLOSED');
OUTPUT
CREATE TABLE
INSERT 0 3
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
SELECT orders.id, orders.amount FROM orders JOIN archive.orders USING (id);
✕ ERROR
ERROR:  42P09: table reference "orders" is ambiguous
LINE 1: SELECT orders.id, orders.amount FROM orders JOIN archive.ord...
               ^
Check the causerun as lab_a
SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders' ORDER BY schemaname;
OUTPUT
 schemaname | tablename
------------+-----------
 archive    | orders
 lab_a      | orders
(2 rows)

pg_tables shows two tables named orders: one in archive, one in lab_a. Both are in the FROM list.

Checkrun as lab_a
SELECT count(*) FROM archive.orders;
OUTPUT
 count
-------
     2
(1 row)
After the fixrun as lab_a
SELECT o.id, o.amount AS current_amount, a.amount AS archived_amount FROM orders o JOIN archive.orders a USING (id) ORDER BY o.id;
✓ SUCCESS
 id | current_amount | archived_amount
----+----------------+-----------------
  1 |            100 |             100
  2 |            200 |             250
(2 rows)

02UPDATE ... FROM with a same-name table

UPDATE orders SET status = 'ARCHIVED' FROM archive.orders WHERE orders.id = archive.orders.id failed: the update target and the FROM table are both orders. Nothing was changed (all rows still OPEN).

With FROM archive.orders a WHERE orders.id = a.id the update ran (UPDATE 2), and ids 1 and 2 became ARCHIVED.

Setup
Setuprun as postgres
DROP SCHEMA IF EXISTS archive CASCADE;
CREATE SCHEMA archive AUTHORIZATION lab_a;
OUTPUT
NOTICE:  00000: schema "archive" does not exist, skipping
DROP SCHEMA
CREATE SCHEMA
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO orders VALUES (1, 100, 'OPEN'), (2, 200, 'OPEN'), (3, 300, 'OPEN');
CREATE TABLE archive.orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO archive.orders VALUES (1, 100, 'CLOSED'), (2, 250, 'CLOSED');
OUTPUT
CREATE TABLE
INSERT 0 3
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
UPDATE orders SET status = 'ARCHIVED' FROM archive.orders WHERE orders.id = archive.orders.id;
✕ ERROR
ERROR:  42P09: table reference "orders" is ambiguous
LINE 1: ...SET status = 'ARCHIVED' FROM archive.orders WHERE orders.id ...
                                                             ^
Check the causerun as lab_a
SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders' ORDER BY schemaname;
OUTPUT
 schemaname | tablename
------------+-----------
 archive    | orders
 lab_a      | orders
(2 rows)

The same two orders tables. The target orders and archive.orders share the reference name orders.

Checkrun as lab_a
SELECT id, status FROM orders ORDER BY id;
OUTPUT
 id | status
----+--------
  1 | OPEN
  2 | OPEN
  3 | OPEN
(3 rows)
After the fixrun as lab_a
UPDATE orders SET status = 'ARCHIVED' FROM archive.orders a WHERE orders.id = a.id;
SELECT id, status FROM orders ORDER BY id;
✓ SUCCESS
UPDATE 2
 id |  status
----+----------
  1 | ARCHIVED
  2 | ARCHIVED
  3 | OPEN
(3 rows)

03DELETE ... USING with a same-name table

DELETE FROM orders USING archive.orders WHERE orders.id = archive.orders.id failed with 42P09 and deleted nothing.

DELETE FROM orders o USING archive.orders a WHERE o.id = a.id deleted 2 rows; only id 3, which is not archived, remained.

Setup
Setuprun as postgres
DROP SCHEMA IF EXISTS archive CASCADE;
CREATE SCHEMA archive AUTHORIZATION lab_a;
OUTPUT
NOTICE:  00000: schema "archive" does not exist, skipping
DROP SCHEMA
CREATE SCHEMA
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO orders VALUES (1, 100, 'OPEN'), (2, 200, 'OPEN'), (3, 300, 'OPEN');
CREATE TABLE archive.orders (id int PRIMARY KEY, amount int, status text);
INSERT INTO archive.orders VALUES (1, 100, 'CLOSED'), (2, 250, 'CLOSED');
OUTPUT
CREATE TABLE
INSERT 0 3
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
DELETE FROM orders USING archive.orders WHERE orders.id = archive.orders.id;
✕ ERROR
ERROR:  42P09: table reference "orders" is ambiguous
LINE 1: DELETE FROM orders USING archive.orders WHERE orders.id = ar...
                                                      ^
Check the causerun as lab_a
SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'orders' ORDER BY schemaname;
OUTPUT
 schemaname | tablename
------------+-----------
 archive    | orders
 lab_a      | orders
(2 rows)

Again archive.orders and lab_a.orders. Writing archive.orders.id does not help, because orders.id on the other side is still ambiguous.

Checkrun as lab_a
SELECT id FROM orders ORDER BY id;
OUTPUT
 id
----
  1
  2
  3
(3 rows)
After the fixrun as lab_a
DELETE FROM orders o USING archive.orders a WHERE o.id = a.id;
SELECT id FROM orders ORDER BY id;
✓ SUCCESS
DELETE 2
 id
----
  3
(1 row)

FAQ

How do I fix "table reference is ambiguous"?

Give each table an alias (FROM orders o JOIN archive.orders a) and qualify columns with the alias, not the table name.

Why is it ambiguous when I wrote archive.orders?

Without an alias, archive.orders is referenced as orders, the same as your own orders. The error points at the unqualified orders.id.

Is 42P09 the same as 42712 table name specified more than once?

No. 42712 is when the same alias is given twice. 42P09 is when two different tables end up with the same reference name and you use it.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12