skip to Main Content

I have a table in a PostgresSQL where I am doing a migration from other database.

I have a id field which is defined by Auto increment using sequence feature in postgres.

Whenever I truncate the table and insert the records, It wont start from 1 instead, it starts last value before truncation.

How to handle it. Whenever I truncate the table, the sequence should start from 1.

Thanks,
Raja Pandi G R

A complete solution for an issue in PostgreSQL

2

Answers


  1. you can reset the sequence manually:

    TRUNCATE TABLE your_table;

    ALTER SEQUENCE your_table_id_seq RESTART WITH 1;

    given your_table_id_seq is the name of the sequence associated with the id column in your table

    Login or Signup to reply.
  2. You can tell TRUNCATE to handle this for you:

    TRUNCATE my_table RESTART IDENTITY;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search