Skip to content

Instantly share code, notes, and snippets.

@scaryguy
Last active October 8, 2024 12:31
Show Gist options
  • Save scaryguy/6269293 to your computer and use it in GitHub Desktop.
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);
@samartioli
Copy link

Should you reindex after changing the primary key?

@prajaktabanne
Copy link

thanks

@rizkidharmawan20
Copy link

Thanks bro!

@amineds
Copy link

amineds commented Nov 15, 2017

helpful ! cheers

@MikilchenkoMarina
Copy link

thank you !!!

@osweet
Copy link

osweet commented Mar 13, 2018

Thanks

@kokovych
Copy link

kokovych commented Aug 2, 2018

Great job 👍

@kaggwachristopher
Copy link

Thanks alot

@saintbyte
Copy link

Спасибо

@vwalker04
Copy link

beautiful! thanks!

@naldi28
Copy link

naldi28 commented Apr 5, 2019

Thank you so much. It's work

@elomariAchraf
Copy link

thanks alot

@S00ahKim
Copy link

thank you!

@mertyertugrul
Copy link

cheers, thank you.

@anoop-sharma
Copy link

Thank you for good explanation.

@jwatte
Copy link

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

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

Thanks!

@TechWithTy
Copy link

ty

@stetodd
Copy link

stetodd commented Feb 17, 2022

Thanks

@marcusflat
Copy link

Thanks a lot !!

@marcbachmann
Copy link

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

thank you

@StephenFlavin
Copy link

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

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

thx, man! it's cool

@ToddEmonster
Copy link

Thank you !

@Pratapchandradeo
Copy link

Save the process bro

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