2

I am using postgresql 9.4. I want to change existing primary key with serial. My query is not working. Anybody know how to do this?

Alter table 'table_name' alter column id BIGSERIAL;

There should be a single query to modify a particular column. I didn't see that

Maha Dev
  • 3,915
  • 2
  • 31
  • 50
  • 1
    https://www.postgresql.org/message-id/AANLkTik2hgCmweE39jemA6p4xPUjh8Gv_QsWdT7uL5UV@mail.gmail.com – Tim Biegeleisen Jan 25 '17 at 09:09
  • ... + don't forget to specify `OWNED BY` clause for `CREATE SEQUENCE` – pozs Jan 25 '17 at 09:15
  • Possible duplicate of [Adding 'serial' to existing column in Postgres](http://stackoverflow.com/questions/9490014/adding-serial-to-existing-column-in-postgres) – pozs Jan 25 '17 at 09:16

1 Answers1

4
CREATE SEQUENCE table_name_id_seq
   OWNED BY table_name.id;

ALTER TABLE table_name
   ALTER id
      SET DEFAULT nextval('table_name_id_seq'::regclass);
Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • Could you please tell me what is regclass stands for. Will it be same while executing this query or it is the data base name ? – sashikanta Mar 03 '17 at 14:42
  • It is an [object identifier type](https://www.postgresql.org/docs/current/static/datatype-oid.html). Essentially, it is a short way to convert an object name to an object identifier (`oid`). – Laurenz Albe Mar 03 '17 at 15:22