Diagnostic Queries
Symptoms
An ALTER TABLE … ADD COLUMN (or a CREATE TABLE) tried to add a column whose name already exists on that table. PostgreSQL reports SQLSTATE 42701 (duplicate_column).
- Most common on non-idempotent
ADD COLUMNmigrations. - The message names both the column and the relation.
- The table is left unchanged.
What the server log shows
ERROR: column "status" of relation "orders" already exists
Why PostgreSQL raises this — what the manual says
the ALTER TABLE reference (ADD COLUMN):
“This form adds a new column to the table, using the same syntax as CREATE TABLE.”
Column names are unique within a table. ADD COLUMN checks pg_attribute for an existing live column of the same name and raises 42701 if found, leaving the table untouched.
Common causes
- A migration that already ran being applied again.
- Two migrations adding the same column.
- The column was added manually outside the migration tool.
How to fix it
- Use
ALTER TABLE orders ADD COLUMN IF NOT EXISTS status text;. - Check the current columns with
\d ordersbefore adding. - tuple/" title="Tuple">Record applied migrations so they do not re-run.
Diagnostic query
-- Does the column already exist on the table?
SELECT attname, atttypid::regtype AS type
FROM pg_attribute
WHERE attrelid = 'orders'::regclass AND attname = 'status' AND NOT attisdropped;
Related & next steps
Reference: PostgreSQL 18 — ALTER TABLE.
Thanks — noted. This helps keep the database accurate.