I need that after TRUNCATE
table, reset his sequence, for this I do :
SELECT setval('mytable_id_seq', 1)
After, when insert rows in table, sequence is started from 2
not 1
How to resert sequence value such, that new first value will be 1
?
SELECT setval('mytable_id_seq', 0) // gives error that value is out of range
First set the minimum value of the sequence
alter sequence mytable_id_seq minvalue 0 start with 1;
Now either reset it:
SELECT setval('mytable_id_seq', 0)
Or reset it while truncating:
truncate mytable restart identity;
Either use the third argument for setval()
:
setval(yourseq, 1, false)
http://www.postgresql.org/docs/current/static/functions-sequence.html
Or alter the sequence:
alter sequence yourseq restart
http://www.postgresql.org/docs/current/static/sql-altersequence.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With