Skip to content

Instantly share code, notes, and snippets.

@pintowar
Created October 18, 2016 12:04
Show Gist options
  • Select an option

  • Save pintowar/110fa10df996397fc0af0a853a743dec to your computer and use it in GitHub Desktop.

Select an option

Save pintowar/110fa10df996397fc0af0a853a743dec to your computer and use it in GitHub Desktop.
Command to generate commands to update the sequence number of all tables of a postgres database
SELECT
'SELECT setval(''' || table_name|| '_id_seq'', COALESCE((SELECT MAX(id)+1 FROM ' || table_name || '), 1), false);' AS command
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema = 'public';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment