Adding serial type to a table - Mailing list pgsql-general

From Scott Marlowe
Subject Adding serial type to a table
Date
Msg-id Pine.LNX.4.33.0204261706090.8484-100000@css120.ihs.com
Whole thread Raw
In response to Speeding query  (Uros Gruber <uros@sir-mag.com>)
List pgsql-general
Someone needed to add a serial type to a table.  Here's the quick dirty,
lazy dba way:

say table t1 has a structure like so:

create table t1 (field1 text, id int);

and data in it, and we want id to be a serial (autoincrementing etc...)
field.

do this:

create table t2 (field1 text, id serial);

Now, assuming that all the data in t1 has a unique id, we can just do
this:

insert into t2 (select * from t1);

and voila, our table is populated.  One small problem, the current value
of the associate sequence is still set to the original number (1 I think).

So, we do this:

select setval('t2_id_seq',(select max(id) from t2));

And now we have our sequence ready to go.

Good luck!


pgsql-general by date:

Previous
From: Jeff Post
Date:
Subject: Help Porting Pg Perl Module
Next
From: Lincoln Yeoh
Date:
Subject: Re: delete column