SoFunction
Updated on 2025-04-08

postgresql implements the start value of the update sequence

To migrate old data to a new database, the id needs to be migrated over, and the sequence value needs to be modified to the current maximum value and add 1.

Use this statement to make the sequence value of s_seq start from 7.

alter sequence s_seq restart with 7;

Supplement: PostgreSQL sets the primary key to increase, PostgreSQL resets the primary key to increase from 1, PostgreSQL clears the table data to increase from 1

PostgreSQL sets the primary key to increase and PostgreSQL resets the primary key to increase and starts with 1

Self-increasing field

Starting from 1

Clear the table data, start from 1

Self-increasing field

PostgreSQL settings to increase primary key

CREATE TABLE student (
 id serial NOT NULL , //serial serial number name varchar(50) COLLATE pg_catalog.default NOT NULL,
 age int4,
 information text COLLATE pg_catalog.default,
 CONSTRAINT _pkey PRIMARY KEY (id) 
);

Starting from 1

PostgreSQL reset primary key increment starts from 1

TRUNCATE student RESTART IDENTITY;

Clear the table data, start from 1

PostgreSQL reset primary key increment starts from 1

//Clear table dataTRUNCATE student;
//Clear the table data, start from 1TRUNCATE student RESTART IDENTITY;

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.