Alex Rivera | Logout

Reset PostgreSQL primary key to 1

Asked 2010-09-29T06:36:46.433
125

Is there a way to reset the primary key of a PostgreSQL table to start at 1 again on a populated table?

Right now it's generating numbers from 1000000 and up. I want it all to reset and start to 1, keeping all my existing data intact.

Edit
Report

1 Answer

275

The best way to reset a sequence to start back with number 1 is to execute the following:

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
answered 2011-03-11T11:12:41.673

Your Answer