thiebo
thiebo

Reputation: 1435

postgresql alter table sequence id starting at a given value

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' ?

Upvotes: 0

Views: 666

Answers (2)

JGH
JGH

Reputation: 17846

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

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

Upvotes: 1

Paco Abato
Paco Abato

Reputation: 4065

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

Upvotes: 0

Related Questions