Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-30 · container image postgres:17
SQLSTATE 42701 (duplicate_column) has two messages. column "id" specified more than once means one statement lists the same name twice. column "last_login" of relation "users" already exists means ADD COLUMN used a name the table already has.
Below are three cases reproduced on PostgreSQL 17 and the fixes that were run.
01CREATE TABLE AS SELECT * from a join (both tables have id)
CREATE TABLE order_report AS SELECT * FROM orders o JOIN customers c ... failed with column "id" specified more than once. A plain SELECT can return two id columns, but a table cannot have them.
The column list showed id in both tables. Listing columns with aliases (o.id AS order_id, c.id AS customer_id) created the table with one row.
Setup
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int, amount int);
INSERT INTO customers VALUES (1, 'Kim');
INSERT INTO orders VALUES (10, 1, 500);CREATE TABLE
CREATE TABLE
INSERT 0 1
INSERT 0 1CREATE TABLE order_report AS
SELECT * FROM orders o JOIN customers c ON c.id = o.customer_id;ERROR: 42701: column "id" specified more than onceSELECT table_name, column_name FROM information_schema.columns WHERE table_name IN ('orders', 'customers') ORDER BY table_name, ordinal_position; table_name | column_name
------------+-------------
customers | id
customers | name
orders | id
orders | customer_id
orders | amount
(5 rows)When saving a join result as a table, list the columns instead of using * and alias names that appear in both tables.
CREATE TABLE order_report AS
SELECT o.id AS order_id, o.amount, c.id AS customer_id, c.name
FROM orders o JOIN customers c ON c.id = o.customer_id;
SELECT * FROM order_report;SELECT 1
order_id | amount | customer_id | name
----------+--------+-------------+------
10 | 500 | 1 | Kim
(1 row)02ALTER TABLE ADD COLUMN run again (migration rerun)
last_login was already added, and running ALTER TABLE users ADD COLUMN last_login timestamptz again failed with column "last_login" of relation "users" already exists.
ADD COLUMN IF NOT EXISTS turned it into a NOTICE (already exists, skipping) and ALTER TABLE finished without error.
Setup
CREATE TABLE users (id int PRIMARY KEY, email text);
ALTER TABLE users ADD COLUMN last_login timestamptz;CREATE TABLE
ALTER TABLEALTER TABLE users ADD COLUMN last_login timestamptz;ERROR: 42701: column "last_login" of relation "users" already existsSELECT column_name, data_type FROM information_schema.columns WHERE table_name = 'users' ORDER BY ordinal_position; column_name | data_type
-------------+--------------------------
id | integer
email | text
last_login | timestamp with time zone
(3 rows)ALTER TABLE users ADD COLUMN IF NOT EXISTS last_login timestamptz;NOTICE: 42701: column "last_login" of relation "users" already exists, skipping
ALTER TABLE03Same column twice in an INSERT column list
INSERT INTO products (id, name, price, price) was meant to be price, stock. The error marks the second price with ^.
With (id, name, price, stock) the row was inserted (INSERT 0 1).
Setup
CREATE TABLE products (id int PRIMARY KEY, name text, price int, stock int);CREATE TABLEINSERT INTO products (id, name, price, price) VALUES (1, 'pen', 1000, 20);ERROR: 42701: column "price" specified more than once
LINE 1: INSERT INTO products (id, name, price, price) VALUES (1, 'pe...
^SELECT column_name FROM information_schema.columns WHERE table_name = 'products' ORDER BY ordinal_position; column_name
-------------
id
name
price
stock
(4 rows)INSERT INTO products (id, name, price, stock) VALUES (1, 'pen', 1000, 20);
SELECT * FROM products;INSERT 0 1
id | name | price | stock
----+------+-------+-------
1 | pen | 1000 | 20
(1 row)FAQ
How do I fix column specified more than once in CREATE TABLE AS?
Replace SELECT * with a column list and give duplicate names aliases, such as o.id AS order_id and c.id AS customer_id.
Is there ADD COLUMN IF NOT EXISTS in PostgreSQL?
Yes. With IF NOT EXISTS an existing column gives a NOTICE and is skipped, so rerunning a migration does not fail.
Is 42701 the same error for INSERT and CREATE TABLE?
Yes, both are duplicate_column. For INSERT the LINE and ^ marker show which column name is repeated.
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