SQLSTATE 42P16 ERROR Class 42: Syntax Error or Access Rule Violation

invalid_table_definition multiple primary keys for table “…” are not allowed — 42P16

PostgreSQL error "multiple primary keys for table "…" are not allowed" (SQLSTATE 42P16): what it means, common causes, and how to fix it.

PG 12, 13, 14, 15, 16, 17, 18 Official docs
Last reviewed May 2025 Grounded in source

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

  1. Keep a single PRIMARY KEY; use composite PK syntax if you need multiple columns.
  2. Use UNIQUE constraints for additional uniqueness requirements.
  3. Drop the existing PK first if you intend to replace it.

Related & next steps

Reference: PostgreSQL 18 Section 5.4 “Constraints”.

Was this helpful?