Diagnostic Queries
Symptoms
A table definition (or ALTER TABLE) declared a primary key when the table already has one. A table can have at most one primary key, so PostgreSQL raises SQLSTATE 42P16 (invalid_table_definition).
- A second PRIMARY KEY was declared on the same table.
- Common with both a column-level and a table-level PK.
- Use UNIQUE for additional uniqueness constraints.
What the server log shows
ERROR: multiple primary keys for table "orders" are not allowed
Why PostgreSQL raises this — what the manual says
Section 5.5.4 Primary Keys:
“A table can have at most one primary key.”
A primary key is the table’s single canonical identifier. Declaring more than one is contradictory, so PostgreSQL rejects the definition with 42P16.
Common causes
- Both a column-level and a table-level PRIMARY KEY clause.
- Adding a PK via ALTER TABLE when one already exists.
- A migration that re-declares the primary key.
How to fix it
- Keep a single PRIMARY KEY; use composite PK syntax if you need multiple columns.
- Use
UNIQUEconstraints for additional uniqueness requirements. - Drop the existing PK first if you intend to replace it.
Related & next steps
Reference: PostgreSQL 18 Section 5.4 “Constraints”.
Thanks — noted. This helps keep the database accurate.