In database migration script (psql) I want to create sequence starting from certain value queried from table, like:
CREATE SEQUENCE book_id_seq START ( SELECT MAX(id) + 1 FROM book.book );
or tried to set \set start (SELECT MAX(id) + 1 FROM book.book)
to use variable like:
CREATE SEQUENCE book_id_seq START :'start';
But using \set
did not inerpret the query.
Another way setting variable did not work also
start := SELECT MAX(id) + 1 FROM book.book;
gave error:
ERROR: syntax error at or near ":="
LINE 1: start := SELECT MAX(id) + 1 FROM book.book;
Selecting value INTO does not help also, because accessing needs another SELECT query.
Using static value works fine:
\set start 33
CREATE SEQUENCE book_id_seq START :'start'::int;
How to use dynamic starting value?
Postgres 9.6