1

I made a primary key called t_id in the table of DB by using the query t_id SERIAL PRIMARY KEY. At the very first time, it started well on 1. Then I deleted all the data and from then, it starts on 2 even though I set it on 1.

Here's the screenshot of what I did on seq - definition on pgAdmin4 :

enter image description here

Anyone has an idea where is the problem ?

Thanks a lot!!

Community
  • 1
  • 1
  • if current value = 1, next value (Which is generated by record insertion into table) will be 2 – Oto Shavadze May 29 '19 at 12:33
  • I tried to set it on 0, then it shows an error msg of setval saying the value should be between 1 and 2147483647 –  May 29 '19 at 12:46
  • Use truncate instead: https://stackoverflow.com/questions/13989243/sequence-does-not-reset-after-truncating-the-table –  May 29 '19 at 13:10

2 Answers2

1

The current value is 1, so the next value that will be served is 2. This is expected.

The doc is helpful on this topic.

Remember that the sequence will always give you a value that was not used before. So if you insert 10 rows, then delete them, the next sequence value will still be 11 (the last served value + 1)

To reset the sequence so the next time it is called it returns 1, you would do

SELECT setval('my_sequence_name', 1, false);  
JGH
  • 15,928
  • 4
  • 31
  • 48
  • I , of course, tried to set it on 0, then it shows an error msg of setval saying the value should be between 1 and 2147483647. –  May 29 '19 at 12:41
0

Your t_id is a primary key auto incremented serial type. These are sequenced, you can set the value of the next sequence using the sequence manipulation functions.

Postgres sequence manipulation functions documentation

Brock
  • 31
  • 5