Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dcube9/064ebc00258730144c86bff5ed75fd6c to your computer and use it in GitHub Desktop.

Select an option

Save dcube9/064ebc00258730144c86bff5ed75fd6c to your computer and use it in GitHub Desktop.
POSTGRESQL - Trasforma una colonna numerica in un indice unico auto incrementato
-- opzionale: assicurati che id sia NOT NULL
ALTER TABLE public.mia_tabella
ALTER COLUMN id SET NOT NULL;
-- aggiungi l'identity (auto-increment)
ALTER TABLE public.mia_tabella
ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY;
SELECT setval(
pg_get_serial_sequence('mia_tabella', 'id'),
(SELECT COALESCE(MAX(id), 0) FROM public.mia_tabella)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment