Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save oleglomako/185df689706c5499612a0d54d3ffe856 to your computer and use it in GitHub Desktop.

Select an option

Save oleglomako/185df689706c5499612a0d54d3ffe856 to your computer and use it in GitHub Desktop.

Revisions

  1. oleglomako revised this gist Mar 13, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion How to add serial to existing column in Postgres
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    SELECT MAX(column_name) + 1 FROM table_name; -- for example max=999
    CREATE SEQUENCE table_name_column_name_seq START WITH 999; -- replace 999 with max above
    ALTER TABLE table_name ALTER COLUMN id SET DEFAULT nextval('table_name_column_name_seq');
    ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT nextval('table_name_column_name_seq');
  2. oleglomako revised this gist May 3, 2020. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions How to add serial to existing column in Postgres
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    SELECT MAX(column_name) + 1 FROM table_name; -- for example max=999

    CREATE SEQUENCE table_name_column_name_seq START WITH 999; -- replace 999 with max above

    ALTER TABLE table_name ALTER COLUMN id SET DEFAULT nextval('table_name_column_name_seq');
  3. oleglomako created this gist May 3, 2020.
    5 changes: 5 additions & 0 deletions How to add serial to existing column in Postgres
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    SELECT MAX(column_name) + 1 FROM table_name; -- for example max=999

    CREATE SEQUENCE table_name_column_name_seq START WITH 999; -- replace 999 with max above

    ALTER TABLE table_name ALTER COLUMN id SET DEFAULT nextval('table_name_column_name_seq');