Skip to content

Instantly share code, notes, and snippets.

@scaryguy
Last active January 16, 2026 18:23
Show Gist options
  • Select an option

  • Save scaryguy/6269293 to your computer and use it in GitHub Desktop.

Select an option

Save scaryguy/6269293 to your computer and use it in GitHub Desktop.
How to change PRIMARY KEY of an existing PostgreSQL table?
-- Firstly, remove PRIMARY KEY attribute of former PRIMARY KEY
ALTER TABLE <table_name> DROP CONSTRAINT <table_name>_pkey;
-- Then change column name of  your PRIMARY KEY and PRIMARY KEY candidates properly.
ALTER TABLE <table_name> RENAME COLUMN <primary_key_candidate> TO id;
-- Lastly set your new PRIMARY KEY
ALTER TABLE <table_name> ADD PRIMARY KEY (id);
@brunojppb
Copy link
Copy Markdown

saved my day! thanks

@jadams74
Copy link
Copy Markdown

👍

@grantrobertsmith
Copy link
Copy Markdown

It's important to note that if the primary key is used as a foreign key constraint in other tables, you'll have to include the keyword CASCADE at the end of the DROP CONSTRAINT command. Subsequently, you will also need to individually recreate the foreign keys in the other tables.

@samartioli
Copy link
Copy Markdown

Should you reindex after changing the primary key?

@prajaktabanne
Copy link
Copy Markdown

thanks

@amineds
Copy link
Copy Markdown

amineds commented Nov 15, 2017

helpful ! cheers

@MikilchenkoMarina
Copy link
Copy Markdown

thank you !!!

@osweet
Copy link
Copy Markdown

osweet commented Mar 13, 2018

Thanks

@kokovych
Copy link
Copy Markdown

kokovych commented Aug 2, 2018

Great job 👍

@kaggwachristopher
Copy link
Copy Markdown

Thanks alot

@saintbyte
Copy link
Copy Markdown

Спасибо

@vwalker04
Copy link
Copy Markdown

beautiful! thanks!

@naldi28
Copy link
Copy Markdown

naldi28 commented Apr 5, 2019

Thank you so much. It's work

@elomariAchraf
Copy link
Copy Markdown

thanks alot

@S00ahKim
Copy link
Copy Markdown

thank you!

@mertyertugrul
Copy link
Copy Markdown

cheers, thank you.

@anoop-sharma
Copy link
Copy Markdown

Thank you for good explanation.

@jwatte
Copy link
Copy Markdown

jwatte commented Jan 16, 2021

If you don't need to rename the columns, you can just do this in a single statement:

ALTER TABLE mytable
DROP CONSTRAINT mytable_pkey,
ADD PRIMARY KEY(lastname, firstname, birthdate); -- or whatever

@venriq
Copy link
Copy Markdown

venriq commented Jan 25, 2021

I'd recommend having some Unique Index backing up the Primary Key before deleting it.
Imagine querying a large table on Production, with millions of rows where the queries depend on the Primary Key (PK) to run efficiently, but suddenly the PK is gone. The queries will pay a high penalty, considering that recreating the PK will take some time depending on the table's size.
Also, always consider creating indices with the CONCURRENTLY option, so the table is not locked.

@DominusKelvin
Copy link
Copy Markdown

Thanks!

@TechWithTy
Copy link
Copy Markdown

ty

@stetodd
Copy link
Copy Markdown

stetodd commented Feb 17, 2022

Thanks

@marcusflat
Copy link
Copy Markdown

Thanks a lot !!

@marcbachmann
Copy link
Copy Markdown

As @venriq mentioned, creating the new index first could be better.
Postgres could reuse other indexes during creation, if there's already one with the same columns.

I'm using that one to switch a multi column index:

ALTER TABLE <table_name> RENAME CONSTRAINT <table_name>_pkey TO <table_name>_pkeyold;
CREATE UNIQUE INDEX <table_name>_pkey ON <table_name> (id, namespace);
ALTER TABLE <table_name> DROP CONSTRAINT <table_name>_pkeyold;
ALTER TABLE <table_name> ADD PRIMARY KEY USING INDEX <table_name>_pkey;

@Ramdelred
Copy link
Copy Markdown

thank you

@StephenFlavin
Copy link
Copy Markdown

worth noting that ALTER TABLE <table_name> DROP CONSTRAINT <table_name>_pkey; does not drop the not null constraint on the column.

@matonga
Copy link
Copy Markdown

matonga commented Dec 21, 2023

@marcbachmann thank you! (didn't know postgres could reuse existing indices for newprimary keys, that's great)

@maksimugus
Copy link
Copy Markdown

thx, man! it's cool

@ToddEmonster
Copy link
Copy Markdown

Thank you !

@Pratapchandradeo
Copy link
Copy Markdown

Save the process bro

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment