1

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?

1
  • If you "migrate" via pg_dump+pg_restore, you won't get this "problem". May 26, 2019 at 17:17

1 Answer 1

1

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

1
  • My choosen solution: SELECT MAX(ticket_id) + 1 FROM tickets_ticket; CREATE SEQUENCE tickets_ticket_a_seq START WITH 25; ALTER TABLE tickets_ticket ALTER COLUMN ticket_id SET DEFAULT nextval('tickets_ticket_a_seq'); May 26, 2019 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.