Resetting Serial Sequence in Postgresql database after import

Posted . Visible to the public.

After doing an import into a postgresql database at times the autogenerated serial id doesn't update - what happens is that if you try to enter a new tuple the id generated would often clash with an existing id. Eg. A table's sequence Id counter is at 10 - you do an import and you then have a 100 new entries. However the sequence serial value is still at 10.

To fix it.

SELECT pg_catalog.setval(pg_get_serial_sequence('table_name', 'id'), (SELECT MAX(id) FROM table_name)+1);
Profile picture of Mohammad Ali
Mohammad Ali
Keywords
postgresql, setval, sequence, serial
Posted by Mohammad Ali to Mohammad Ali's deck (2013-02-11 08:36)