[COLUG] Postgres Auto-Increment

Mark Erbaugh mark at microenh.com
Sat Oct 27 12:29:27 EDT 2007


I am importing data from another database into Postgres (v 8.1).  The
current data has an auto incrementing field which I need to preserve.

I've created the Postgres tables with SERIAL fields which create a
sequence and uses the next value from the sequence as a default for
these fields, which should work.

However, after importing the data, the sequence still returns 1 as the
next value. I guess I need to issue:

ALTER SEQUENCE (name) RESTART WITH (value)

Where (value) is the highest value in the SERIAL field, but I need to do
this for many tables (and thus sequence names and SERIAL field names).
Is there an automatic way to re-sync a sequence with the relevant table?

Thanks,
Mark



More information about the colug432 mailing list