How to reset Postgres SERIAL number
When testing a table with a SERIAL
field, this number will grow even if you remove all items in the table (like you’d do during testing), so you might insert a value and its id is 15
for example.
To restart the sequence from 1
, run the SQL query:
ALTER SEQUENCE TABLENAME_id_seq RESTART
Replace TABLENAME
with your table name, like:
ALTER SEQUENCE users_id_seq RESTART
Using TablePlus you have the option to reset the sequence when you truncate a table to remove its content.
→ I wrote 17 books to help you become a better developer:
- C Handbook
- Command Line Handbook
- CSS Handbook
- Express Handbook
- Git Cheat Sheet
- Go Handbook
- HTML Handbook
- JS Handbook
- Laravel Handbook
- Next.js Handbook
- Node.js Handbook
- PHP Handbook
- Python Handbook
- React Handbook
- SQL Handbook
- Svelte Handbook
- Swift Handbook
Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025