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!
Create the sequence, then set the value afterwards:

create sequence some_seq;
select setval('some_seq', (select max(id) from some_table));


You probably also want to "link" the sequence to the table's column as well: 

alter sequence some_seq
  owned by some_table.id;




For modern Postgres version (>= 10), it is recommended to use an identity column instead:

alter table some_table
   alter id add generated always as identity;

select setval(pg_get_serial_sequence('some_table', 'id'), (select max(id) from some_table));