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);
@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