How to reset sequence in postgres and fill id column with new data ?

How to reset sequence in postgres and fill id column with new data ?

Asked on January 9, 2019 in PostgreSQL.
Add Comment


  • 3 Answer(s)

    User don’t want to retain the ordering of function , then do that

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

    This is simple way to use to order without recreation the whole table.

    Answered on January 9, 2019.
    Add Comment

    This query  use to reset sequence:

    SELECT setval('sequence_name', 0);
    

    This query use to update current record :

    UPDATE foo SET id = DEFAULT;
    
    Answered on January 9, 2019.
    Add Comment

    PostgreSQL 8.4 or newer there is no have to particular the WITH 1 anymore. The start value that was recorded by CREATE SEQUENCE or last set by ALTER SEQUENCE START WITH will be used (most probably this will be 1).

    Reset sequence:

    
    ALTER SEQUENCE seq RESTART;
    
    

    Then update table:

    
    UPDATE foo SET id = DEFAULT;
    
    

    Refer this source :

    PostgreSQL Docs

     

    Answered on January 9, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.