How to start a serial id from a custom number (e.g. like 1001)

Hi I have a simple table called PRODUCT. I want the primary key id starts from 1001 not from 1 this is my table
export const product = pgTable('product', {
id: serial('id').primaryKey(),
productName: text('product_name').notNull();
export const product = pgTable('product', {
id: serial('id').primaryKey(),
productName: text('product_name').notNull();
5 Replies
Sillvva
Sillvva8mo ago
/* alter sequence table_column_seq restart with value; */
alter sequence product_id_seq restart with 1001;
/* alter sequence table_column_seq restart with value; */
alter sequence product_id_seq restart with 1001;
reza_rahem
reza_rahemOP8mo ago
thanks, but I used this and put it into a sql file then generate it but didn't work. would please explain more
Sillvva
Sillvva8mo ago
This is probably the best explanation I can find. https://popsql.com/learn-sql/postgresql/how-to-alter-sequence-in-postgresql
PopSQL
PostgreSQL: Alter Sequence Command - PopSQL
Learn how to use the PostgreSQL 'Reset Sequence' command. If you have auto-incrementing serial ID columns, they typically start at 1. Sequences, managed in their designated tables, store essential details like start values and last values. Use 'Alter Sequence' to change the initial ID number.
reza_rahem
reza_rahemOP8mo ago
thanks again but when I generate or push, this won't work HOW should I implement this?
Sillvva
Sillvva8mo ago
If you have an SQL client to connect to your db. Try to run it there and then run the SELECT command on it to confirm it was updated. Note that it does not update existing values in the db.
select * from product_id_seq;
select * from product_id_seq;
Want results from more Discord servers?
Add your server