Skip to content

Instantly share code, notes, and snippets.

@hasanisaeed
Created October 3, 2021 23:39
Show Gist options
  • Save hasanisaeed/9afa335ee828986d052911e6a5958a2f to your computer and use it in GitHub Desktop.
Save hasanisaeed/9afa335ee828986d052911e6a5958a2f to your computer and use it in GitHub Desktop.
-- 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