Reset SERIAL counter for a PostgreSQL database table
For the table my_table
with a SERIAL column my_id
.
SELECT SETVAL(
pg_get_serial_sequence('my_table', 'my_id'),
(SELECT COALESCE(MAX(my_id), 0) FROM my_table)
);
This above command sets the counter to the current maximum value of our SERIAL column. Run it to return to normal operations! The addition of COALESCE makes it work even with empty tables; the counter is set to zero.