How to reset a sequence with PostgreSQL

Sun Nov 02 01:49:37 -0800 2008

I would think that

1
2
ALTER SEQUENCE sequence_name
      RESTART WITH (SELECT max(id) FROM table_name);

would work, but it doesn’t. Use:


SELECT SETVAL('sequence_name', (SELECT MAX(id) FROM table_name) + 1);

instead and you will be a lot more happier.

blogLater

Mikel

  1. Jo Budd Says:

    Good stuff.
    SELECT setval(‘your_table_id_seq’, (SELECT MAX FROM your_table)+1);
    I’m using this when change a selected primary key sequence. Same as you suggested. ;)
    Regards,
    Jonathan Budd

  2. dfhdfg5r Says:

    I don’t agree that Rails is getting bloated. I see that the example you have provided is a treat. I have my doubts by the number of literal aliases being reduced, would it work perfect. I have to get some coop systems reviews, where I can get them.weidaileix dh enhozahuosh gf wiekesaoresy jd sybgosegwod
    Please post them if you have any.

  3. jibran Says:

    The best person to give you medical advice about liver disease is your doctor. Best thing we can do is recommend perhaps a good doctor if you need a second or third opinion. casino

Leave a Reply