Resetting ID Sequence in PostgreSQL

Reading Time: 1 minute — Latest Inserted ID SELECT MAX(IDFIELD) FROM TABLENAME; — The id that will be assigned to the upcoming data tulip. — The result shoult be Latest ID + 1 SELECT nextval(‘TABLENAME_ID_SEQ’); BEGIN; LOCK TABLE TABLENAME IN EXCLUSIVE MODE; SELECT setval(‘TABLENAME_ID_SEQ’, COALESCE((SELECT MAX(IDFIELD)+1 FROM TABLENAME), 1), false); COMMIT;