Search code examples
postgresqlprimary-keyrestoreautonumber

postgresql serial pk reverts to integer after restore


I built a db with serial type for the pks, I migrated to another server and the pk columns are now integer and as a result I cannot add new data due to the not null restriction of a pk. Is there any Alter command which can fix this?


Solution

  • SERIAL is not a data type in postgresql just a convenience word when creating tables that makes the column an integer type and adds auto-incrementing. All you have to do is add back auto-incrementing (a sequence) to the column and make sure its next value is greater than anything in the table.

    This question covers adding serial to an existing column

    This answer explains how to reset the counter