We have a table in a Postgres 14 DB which id was created using SERIAL and we want to migrate to using IDENTITY instead. That table already has a lot of entries in prod. How can we safely migrate to IDENTITY?
I did some research and found a script that might help, but after testing it, the results were not as I expected...
INSERT INTO public.another_table (<columns>) VALUES (<values>);
ALTER TABLE public.another_table
ALTER COLUMN id DROP DEFAULT;
DROP SEQUENCE public.another_table_id_seq;
ALTER TABLE public.another_table
ALTER COLUMN id SET DATA TYPE INT;
ALTER TABLE public.another_table
ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY;
INSERT INTO public.another_table (<columns>) VALUES (<values>);
If you do something like that, postgres will complain with the following message: ERROR: duplicate key value violates unique constraint
.