1

I'm migrating from MySQL to Postgres with mysqldump and psql. I copied all the data but the serials start at 1. I need to update the starting value to the last id number. How can I script this to do it automatically? I tried

alter sequence keyword_id_seq restart (select max(id) from keyword);

It gave an error

ERROR:  syntax error at or near "(" at character 39
STATEMENT:  alter sequence keyword_id_seq restart (select max(id) from keyword);
ERROR:  syntax error at or near "("
LINE 1: alter sequence keyword_id_seq restart (select max(id) from k...

I can't set the start value when creating the sequence (with CREATE SEQUENCE table_colname_seq START 123;) because new values may be inserted into the old DB while I'm editing the file or before the data is copied.

1 Answer 1

1

Use setval.

SELECT setval('keyword_id_seq', (select max(id) from keyword));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.