Sequence does not reset after truncating the table Sequence does not reset after truncating the table postgresql postgresql

Sequence does not reset after truncating the table


Use the TRUNCATE SQL command.

For a single table the syntax is the following:

TRUNCATE TABLE table_name RESTART IDENTITY;

For multiple tables:

TRUNCATE TABLE table_foo, table_bar RESTART IDENTITY;

What it does:

Automatically restart sequences owned by columns of the truncated table(s).

Details here: TRUNCATE @ postgresql.org


Following is the standard way to reset sequence:

truncate table table_name restart identity;

but in some version & platform, it's syntax error,

in that case, you can truncate without reset sequence, and alter the sequence with another sql, try this:

truncate table table_name;alter sequence seq_name start 1;


The best way to reset a sequence to start back with number 1 is to execute the following after you have successfully truncate it:

ALTER SEQUENCE <tablename>_<id>_seq RESTART WITH 1

So, for example for the users table it would be:

ALTER SEQUENCE users_id_seq RESTART WITH 1