Skip to content

Instantly share code, notes, and snippets.

@yann2192
Created July 29, 2013 09:41
Show Gist options
  • Save yann2192/6103258 to your computer and use it in GitHub Desktop.
Save yann2192/6103258 to your computer and use it in GitHub Desktop.
(django) Resync all table sequences in postgres
DO $$
DECLARE
tables CURSOR FOR SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg_%' ORDER BY tablename;
BEGIN
FOR table_record IN tables LOOP
BEGIN
EXECUTE 'SELECT setval('''|| table_record.tablename ||'_id_seq'', (SELECT MAX(id) FROM '||table_record.tablename||')+1)';
EXCEPTION
WHEN OTHERS THEN
END;
END LOOP;
END$$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment