Haikson

[ Everything is possible. Everything takes time. ]

How to create new column in existing postgresql table as primary key

Hi. Here is the solution How to create new column in existing postgresql table as primary key.

If You have an exisiting table in PostgreSQL database and You want add new column as primary key and autoincrement, You can run this SQL commands

create sequence scheme_name.seq_id_new_column;

alter table scheme_name.t_table_name
    add column id_new_column bigint primary key not null DEFAULT nextval('scheme_name.seq_id_new_column');

First creates sequence seq_id_new_column in scheme scheme_name

Second creates new column id_new_column and fills wit nextval('scheme_name.seq_id_new_column')