0

I want to restart sequence with max+1 of certain table.

SELECT max(id)+1 
    INTO testVal
FROM project;

ALTER SEQUENCE project_id_seq RESTART testVal;

This gives syntax error at testVal. Can someone please explain me what is a problem, propose alternative solution?

1 Answer 1

2

for sequences you should be using setval

SELECT SETVAL('project_id_seq', (SELECT max(id)+1 FROM project))

https://www.postgresql.org/docs/current/functions-sequence.html

Sign up to request clarification or add additional context in comments.

3 Comments

Oh, thats way cleaner, tested and seems to be working properly. Thank you. BTW would you know why my solution was not working? Just to take a lesson from it.
Your ALTER SEQUENCE referred only to the table name, however it would require a subquery to select that column and I do not believe you could do that - also SELECT [columns] INTO [tablename] creates a table with that data inside but since you did not give max(id)+1 an alias it would default a name to something like ?column?
Okay, I think I understand. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.