
It will give you the same result as you were expecting from using serial. The second line alters your table with the new default value, which will be determined by the previously created sequence.

The START statement defines what value this sequence should start from.

In your case the table is address and the column is new_id. ahorsewithnoname at 18:27 2 I think you can create new table with updated COLUMN and copy data, this might be a faster way. But I doubt you will get any substantial size improvement (reduction) with that anyway. ahorsewithnoname at 18:27 2 I think you can create new table with updated COLUMN and copy data, this might be a faster way. 1 That change requires the entire table to re-written. I suppose this was just too easy to have had a chance of working. OWNED BY statement connects the newly created sequence with the exact column of your table. 1 That change requires the entire table to re-written. In pgsql, is there a way to have a table of several values, and choose one of them (say, otherid), find out what its highest value is and make every new entry that is put in the table increment from that value. The first line of the query creates your own sequence called my_serial.
Postgres alter column data type serial#
In case you would like to achieve the same effect, as you are expecting from using serial data type when you are altering existing table you may do this: CREATE SEQUENCE my_serial AS integer START 1 OWNED BY address.new_id ĪLTER TABLE address ALTER COLUMN new_id SET DEFAULT nextval('my_serial') Because serial is not a true data type, but merely an abbreviation or alias for a longer query. If you'll try to ALTER an existing table using this data type you'll get an error. This happened because you may use the serial data type only when you are creating a new table or adding a new column to a table.
