Search code examples
sqlruby-on-railspostgresqlpgadmin

Postgresql 9.4, Make existing primary key as SERIAL


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


Solution

  • 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);