postgresql alter table sequence id starting at a given value

587 Views Asked by At

The 'id' from my table 'biblios' didn't autoincrement. It had already data in it and the 'id' go from 112 to 175.

I altered the table to add nextval, doing this:

CREATE SEQUENCE biblios_id_seq
OWNED by biblios.id;
ALTER TABLE biblios
ALTER id
SET DEFAULT nextval('biblios_id_seq'::regclass);

This starts the id at '1'.

How do I make the autoincrement continue at '176' ?

2

There are 2 best solutions below

0
On

Use MINVALUE minvalue:

CREATE SEQUENCE biblios_id_seq
MINVALUE 176
OWNED by biblios.id;
ALTER TABLE biblios
ALTER id
SET DEFAULT nextval('biblios_id_seq'::regclass);
0
On

Since the sequence is already created, you can reset it using

SELECT setval('biblios_id_seq', max(id)) FROM biblios;