Forked from slavamokerov/creating-sequence-postgres.sql
Created
September 14, 2022 09:09
-
-
Save ldonjibson/a41c1a9305afd594826ec68a67743279 to your computer and use it in GitHub Desktop.
Creating sequence in an existing table. PostgreSQL
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
/* | |
objects_id_object_seq - sequence name | |
objects - table name | |
id_object - ID field name | |
seq - standard postfix | |
Doc for setval() and nextval(): https://www.postgresql.org/docs/current/functions-sequence.html | |
*/ | |
CREATE SEQUENCE IF NOT EXISTS objects_id_object_seq; -- IF NOT EXISTS is works only in Postgres 9.5+ | |
SELECT setval( | |
'objects_id_object_seq', | |
(SELECT max(id_object) FROM objects) | |
); | |
ALTER TABLE objects ALTER COLUMN id_object SET DEFAULT nextval('objects_id_object_seq'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment