PostgreSQL – How to reset a sequence – with alter sequence restart or setval SQL function

Two ways:

1.) alter sequence restart

2.) select setval();

Both show below, both work but setval is preferred as it doesn’t require a blocking lock.

create sequence public.junk_seq start with 1;

select nextval('junk_seq');

alter sequence public.junk_seq restart with 50;

select nextval('junk_seq');

-- or

select setval('junk_seq',200);

select nextval('junk_seq');

Leave a Comment

Scroll to Top