PostgreSQL: starting a sequence at MAX(the_column)+1
<p>I want to add a sequence to a column that might already have data, so I'm trying to start it beyond whatever's already there. Assuming there already is data, I would like to have done it this way:</p>

<pre><code>CREATE SEQUENCE my_sequence MINVALUE 1000000 START
    (SELECT MAX(id_column) FROM my_table) OWNED BY my_table.id_column;
</code></pre>

<p>but it keeps dying at <code>(</code> claiming syntax error. It's like the start value has to be cold hard numbers--nothing symbolic.</p>

<p>Of course, an even better solution would be if the sequence could be intelligent enough to avoid duplicate values, since <code>id_column</code> has a unique constraint on it--that's why I'm doing this. But from what I can tell, that's not possible.</p>

<p>I also tried skipping the <code>START</code> and then doing:</p>

<pre><code>ALTER SEQUENCE my_sequence RESTART WITH (SELECT max(id_column)+1 FROM my_table);
</code></pre>

<p>but, again, it doesn't seem like to symbolic start values.</p>

<p>I'm running PostgreSQL 9.4 but some of our customers are using stuff as primitive as 8.3.</p>