Jens Gössing
Jens Gössing

Reputation: 1

Use max value of a column to restart postgresql sequence

I need to restart the table sequence after manually importing data and want to dynamically use the maximum value (+1) currently present in the id column.

I identify the value with

SELECT coalesce(MAX("id")+1) FROM "termine";

And I update the sequence with

ALTER SEQUENCE termine_id_seq RESTART WITH 123;

How can I use the MAX id within the ALTER instead of the "123"?

Upvotes: 0

Views: 677

Answers (1)

Jasen
Jasen

Reputation: 12392

Use the setval helper function.

select pg_catalog.setval('termine_id_seq'::regclass, MAX("id"),true) FROM "termine";

Upvotes: 2

Related Questions