4

Is there any way to reset all the sequences of tables, when truncate a table on cascade.

I already read this post How to reset sequence in postgres and fill id column with new data?

ALTER SEQUENCE seq RESTART WITH 1;
UPDATE t SET idcolumn=nextval('seq');

It work only for one sequence, but my problem is to restart all the sequence of the truncated tables.

Consider when I use TRUNCATE sch.mytable CASCADE; It affect 3 related tables, which mean three sequences, Is there any solution to restart this sequence in one shot.

11

The TRUNCATE statement has an additional option RESTART IDENTITY which resets the sequences associated with the table columns.

TRUNCATE sch.mytable RESTART IDENTITY CASCADE;

If CASCADE is defined, then the sequences of all affected tables are reset.

  • thank you @a_horse_with_no_name I already use this my bad, I think I don't use refresh for that I don't notice the changes, now I refresh it and It work :) – YCF_L Dec 19 '17 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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