PostgreSQL Cheat Sheet/Add primary key

Define a primary key after table creation.

Section: Indexes and Constraints

Add primary key

sql
sql
ALTER TABLE invoices ADD CONSTRAINT invoices_pkey PRIMARY KEY (invoice_id);
Explanation

See summary for usage details.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Indexes and Constraints
Add foreign key
Link rows to a parent table with referential integrity.
OpenIn sheetsqlsame section
Add unique constraint
Ensure unique values through a table constraint.
OpenIn sheetsqlsame section
Create index
Add a basic B-tree index to speed lookups.
OpenIn sheetsqlsame section
Create index concurrently
Build an index with reduced write blocking.
OpenIn sheetsqlsame section
Create unique index
Enforce uniqueness with an index.
OpenIn sheetsqlsame section
Create partial index
Index only a subset of rows.
OpenIn sheetsqlsame section