Created
October 3, 2021 23:39
-
-
Save hasanisaeed/9afa335ee828986d052911e6a5958a2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Converting an int to a serial more or less only means adding a sequence default to the value, so to make it a serial; | |
-- Pick a starting value for the serial, greater than any existing value in the table | |
SELECT MAX(id)+1 FROM mytable | |
-- Create a sequence for the serial (tablename_columnname_seq is a good name) | |
CREATE SEQUENCE test_id_seq MINVALUE 3 (assuming you want to start at 3) | |
-- Alter the default of the column to use the sequence | |
ALTER TABLE test ALTER id SET DEFAULT nextval('test_id_seq') | |
-- Alter the sequence to be owned by the table/column; | |
ALTER SEQUENCE test_id_seq OWNED BY test.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment