Skip to content

Instantly share code, notes, and snippets.

@domguard
Created December 1, 2011 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domguard/1414936 to your computer and use it in GitHub Desktop.
Save domguard/1414936 to your computer and use it in GitHub Desktop.
Remettre les séquences à zéro sur PostGreSQL
/usr/bin/psql --host localhost --port 5432 --username db_owner db_name
CREATE OR REPLACE FUNCTION "reset_sequence" (tablename text) RETURNS "pg_catalog"."void" AS
$body$
DECLARE
BEGIN
EXECUTE 'SELECT setval( '''
|| tablename
|| '_id_seq'', '
|| '(SELECT id + 1 FROM "'
|| tablename
|| '" ORDER BY id DESC LIMIT 1), false)';
END;
$body$ LANGUAGE 'plpgsql';
select sequence_name, reset_sequence(split_part(sequence_name, '_id_seq',1)) from information_schema.sequences where sequence_schema='public';
\q
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment