PostgreSQL 9.x Sequence Fixing Script
This script is intended to automatically fix the sequence numbers for all tables in the current database.
This is accomplished through the use of the
setval() command, which we provide with the next ID value we wish to make use of.
We use the
setval(sequence, number, is_called) overload
is_called = false in conjunction with
COALESCE(MAX + 1, 1) to ensure that, with an empty table, the next sequence
1 as expected.
Save below file as
fix-sequences.sql, then run
psql -f fix-sequences.sql