0

How to start my id column from 1 ?

My tabel is :

 id |   origin    | destination | duration
----+-------------+-------------+----------
  6 | Paris       | New York    | 540
  7 | Tokyo       | Shanghai    | 185
  8 | Seoul       | Mexico City | 825
  9 | Mexico City | Lima        | 350
 10 | Hong Kong   | Shanghai    | 130

Here id is my SERIAL KEY and PRIMARY KEY.

And I am using PostgreSQL.

  • Does this answer your question? [Reset PostgreSQL primary key to 1](https://stackoverflow.com/questions/3819292/reset-postgresql-primary-key-to-1) – A ツ May 14 '20 at 19:40

1 Answers1

2

Serial columns are linked to a SEQUENCE. If it is an automatically generated sequence it will be called something like tablename_columnname_id_seq. You can alter a sequences next value using the function SETVAL(sequence_name, number).

However, as you have existing data in the system you will end up with conflicts when the sequence hits 6. You have to update the id column of your existing table, then set the sequence to the biggest value so the next table entry continues the sequence correctly.

Dave A
  • 36
  • 3
  • Example how to call SETVAL in SQL script: `PERFORM SETVAL('table_name_id_seq', 1);` NOTE: SELECT statement is not suitable here, because it gets error message: **ERROR: query has no destination for result data**, so PERFORM is used instead – Johnny May 16 '23 at 14:01