Skip to content

Instantly share code, notes, and snippets.

@maptastik
Created June 27, 2019 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maptastik/27dcbf7d9b265f2ae2855f0f35134c32 to your computer and use it in GitHub Desktop.
Save maptastik/27dcbf7d9b265f2ae2855f0f35134c32 to your computer and use it in GitHub Desktop.
-- See https://stackoverflow.com/a/15748896/3163905
-- Create a new sequence
CREATE sequence <column_id_seq>;
-- Alter the field you're converting to an auto-incrementing field be setting a default value based on a sequence
ALTER TABLE <table_name> alter <id_column> SET DEFAULT nextval(<'id_column_seq'>);
-- Set the current max value of the id column as the starting point for the sequence
SELECT setval(<'id_column_seq'>, <max_id_column_value> );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment