How to loop through PostgreSQL Sequnces?

Hello all,

When removing all data from tables, I want to reset the squence counter back to 1.

This is the code that works:

ALTER SEQUENCE transactions_rowid_seq RESTART WITH 1;

I need to automate this so was wondering how to pull the sequences from Postgres automatically, then use them inside of a loop.

Any ideas??
Thanks,
Tim

Query information_schema.sequences.

Thank you Kem!
Tim

You could also use truncate and set the “RESTART IDENTITY” flag as described here:

3 Likes