40

I've a little problem with serial : From a file, I filled my database in which I have a client ID (it is a serial and it is my primary key). I have 300 clients so 300 client ID (1 to 300). Now my problem is, I've a form for new clients.I cannot add them because when I add a client, my program adds the client with ID 1 or the ID 1 is already assigned to another client.

So my question is : is it possible to change the starting value of a serial for to resolve this problem ?

0

5 Answers 5

58

You can alter a sequence using RESTART WITH to change the current sequence number;

ALTER SEQUENCE test_seq RESTART WITH 300;

To get the sequence name if you created it using the serial keyword, use

SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'table name goes here'); 

An SQLfiddle to test with.

4
  • So for to change the starting value, I need to create a sequence.
    – afk
    May 3, 2013 at 15:58
  • 2
    @user1086267: a serial column is populated by a sequence. Please do read the manual. This is all documented there.
    – user330315
    May 3, 2013 at 16:02
  • 2
    @user1086267 To get the sequence name if you created it using the serial keyword, use SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'table name goes here'); May 3, 2013 at 16:03
  • 5
    adsrc column is no longer present in latest posgres Mar 18, 2020 at 6:22
18

PostgreSQL

ALTER SEQUENCE tablename_columnname_seq RESTART WITH anynumber;

Example:

ALTER SEQUENCE test_table_rec_id_seq RESTART WITH 4615793;
4

if your Postgresql version is higher than the upper answer, you could try getting serial key with select pg_get_serial_sequence('ingredients', 'id');

and SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'ingredients');

3

For those who try to point to specific schema's table, here's the query you will need to execute.

// To get the sequence name
SELECT pg_get_serial_sequence('"yourSchema"."yourTable"', 'yourColumn');
//Output: yourSchema."yourTable_yourColumn_seq"

ALTER SEQUENCE yourSchema."yourTable_yourColumn_seq" RESTART WITH 100;
-1

The solutions above did not work for what I needed.

I needed a serial id to use as a primary key that started from 1000, rather than 1.

In order to do this, I created a standard serial column:

ALTER table my_table ADD COLUMN new_id SERIAL PRIMARY KEY;

and then updated that column:

UPDATE my_table set new_id = new_id + 1000;

I then joined that table to the table with existing non-consecutive id numbers under 1000.

1
  • yeah @aclong I'm not sure why the downvote either. I can say a year later than i never ran into any problems with this method. Perhaps it would be better to use the combination of two columns as the p key. It could be PRIMARY KEY (source, serial_column) where source was ['source_1', 'source_2'] and serial_column was the union of two serial columns starting from 1 so that 'source_1, 1 would be unique from source_2, 1 but that would not have worked as well for my purposes. You can upvote if you like :) Jun 29, 2021 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.