Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres: How to set column default value as another column value while altering the table

Tags:

sql

postgresql

I have a postgres table with millions of record in it. Now I want to add new column to that table called "time_modified" with the value in another column "last_event_time". Running a migration script is taking long time , so need a simple solution to run in production.

like image 758
Nagaraj Vittal Avatar asked Mar 13 '26 10:03

Nagaraj Vittal


1 Answers

Assuming that the columns are timestamps you can try:

alter table my_table add time_modified text;
alter table my_table alter time_modified type timestamp using last_event_time;
like image 134
klin Avatar answered Mar 14 '26 23:03

klin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!