user11492726
user11492726

Reputation:

postgreSQL: primary key doesn't start on 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!!

Upvotes: 1

Views: 3359

Answers (2)

Brock
Brock

Reputation: 31

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

Upvotes: 0

JGH
JGH

Reputation: 17846

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);  

Upvotes: 1

Related Questions