Notification texts go here Contact Us Buy Now!

How to reset sequence in postgres and fill id column with new data?

To reset the sequence in PostgreSQL and fill the ID column with new data, you can use a series of SQL statements:

  • Disable Triggers:
ALTER TABLE table_name DISABLE TRIGGER ALL;
  • Restart Sequence:
ALTER SEQUENCE sequence_name RESTART WITH 1;
  • Update ID Column:
UPDATE table_name SET id = nextval('sequence_name');
  • Enable Triggers:
ALTER TABLE table_name ENABLE TRIGGER ALL;

This approach ensures that the sequence is reset and the ID column is updated with new, sequential values while preserving the order of existing rows.

Alternatively, if you don't need to retain the ordering of IDs, you can use a simpler method:

  • Restart Sequence:
ALTER SEQUENCE sequence_name RESTART WITH 1;
  • Update ID Column:
UPDATE table_name SET id = nextval('sequence_name');

This method will also reset the sequence and update the ID column with new values, but it will not preserve the order of the rows.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.