Categories
Database Development

Create sequence starting with maximum id from a table

I’m trying to create a sequence in the following way:

create sequence some_seq start with (select max(id) + 1 from some_table);

But I’m getting the following error:

ERROR: syntax error at or near "("

I’ve also tried this approach:

DO $$
DECLARE
    min_seq_value int8 := (select max(id) + 1 from some_table);
BEGIN
    create sequence some_seq start with min_seq_value;
END; $$;

But it gives me:

ERROR: syntax error at or near "min_seq_value"

It seems to be pretty basic but I can’t get it working. Any ideas?

Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *