Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
A PostgreSQL table can have only one primary key. Declaring a second one — in CREATE TABLE or with ALTER TABLE ... ADD PRIMARY KEY — fails with 42P16. In a CREATE TABLE the caret points at the second PRIMARY KEY.
Three real-world versions are below: writing PRIMARY KEY after two columns when a composite key was meant, adding a primary key to a table that already has one, and trying to switch the primary key to another column.
Each needed a different fix: a table-level composite key, a UNIQUE constraint instead, or dropping the old primary key first.
01PRIMARY KEY written after two separate columns
CREATE TABLE order_items (order_id int PRIMARY KEY, line_no int PRIMARY KEY, product text) failed with 42P16, pointing at the second PRIMARY KEY. Two column-level PRIMARY KEYs mean two primary keys, not one composite key.
Writing it once at table level — PRIMARY KEY (order_id, line_no) — created the table with order_items_pkey on both columns.
CREATE TABLE order_items (order_id int PRIMARY KEY, line_no int PRIMARY KEY, product text);ERROR: 42P16: multiple primary keys for table "order_items" are not allowed
LINE 1: ...rder_items (order_id int PRIMARY KEY, line_no int PRIMARY KE...
^CREATE TABLE order_items (order_id int, line_no int, product text, PRIMARY KEY (order_id, line_no));CREATE TABLESELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'order_items'::regclass; conname | pg_get_constraintdef
------------------+---------------------------------
order_items_pkey | PRIMARY KEY (order_id, line_no)
(1 row)02ADD PRIMARY KEY on a table that already has one
customers already had id as its primary key. ALTER TABLE customers ADD PRIMARY KEY (email) failed with 42P16.
pg_constraint showed customers_pkey = PRIMARY KEY (id). When the goal is only to stop duplicate emails, ADD CONSTRAINT customers_email_key UNIQUE (email) succeeded and left the id key in place.
Setup
CREATE TABLE customers (id int PRIMARY KEY, email text NOT NULL);
INSERT INTO customers VALUES (1, 'kim@example.com'), (2, 'lee@example.com');CREATE TABLE
INSERT 0 2ALTER TABLE customers ADD PRIMARY KEY (email);ERROR: 42P16: multiple primary keys for table "customers" are not allowedSELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'customers'::regclass AND contype = 'p'; conname | pg_get_constraintdef
----------------+----------------------
customers_pkey | PRIMARY KEY (id)
(1 row)ALTER TABLE customers ADD CONSTRAINT customers_email_key UNIQUE (email);ALTER TABLE03Changing the primary key to another column
To make sku the primary key of products instead of id, ADD CONSTRAINT products_sku_pkey PRIMARY KEY (sku) failed with 42P16 because products_pkey still existed.
After ALTER TABLE products DROP CONSTRAINT products_pkey, the same ADD CONSTRAINT succeeded and the table had only products_sku_pkey = PRIMARY KEY (sku).
Setup
CREATE TABLE products (id int PRIMARY KEY, sku text NOT NULL, name text);
INSERT INTO products VALUES (1, 'A-100', 'Pen'), (2, 'A-200', 'Note');CREATE TABLE
INSERT 0 2ALTER TABLE products ADD CONSTRAINT products_sku_pkey PRIMARY KEY (sku);ERROR: 42P16: multiple primary keys for table "products" are not allowedALTER TABLE products DROP CONSTRAINT products_pkey;ALTER TABLEALTER TABLE products ADD CONSTRAINT products_sku_pkey PRIMARY KEY (sku);
SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'products'::regclass;ALTER TABLE
conname | pg_get_constraintdef
-------------------+----------------------
products_sku_pkey | PRIMARY KEY (sku)
(1 row)FAQ
How do I make a composite primary key in PostgreSQL?
Write it once at table level: PRIMARY KEY (order_id, line_no). Writing PRIMARY KEY after each column gives 42P16.
How do I add a second unique key to a table with a primary key?
Use UNIQUE instead: ALTER TABLE customers ADD CONSTRAINT customers_email_key UNIQUE (email);
How do I change the primary key column?
Drop the current one (ALTER TABLE products DROP CONSTRAINT products_pkey), then add the new one.
What is the name of the existing primary key?
Usually <table>_pkey. Check with SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint WHERE conrelid = 'customers'::regclass AND contype = 'p';
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