Created
July 29, 2013 09:41
-
-
Save yann2192/6103258 to your computer and use it in GitHub Desktop.
(django) Resync all table sequences in postgres
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
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