I want to add a new column that is NULL
for existing rows, but has default value for new rows.
This:
ALTER TABLE foo ADD COLUMN bar timestamp NULL DEFAULT clock_timestamp();
Won’t work, because it would add default value to existing rows.
This would work:
ALTER TABLE foo ADD COLUMN bar timestamp NULL;
ALTER TABLE foo ALTER bar SET DEFAULT clock_timestamp();
but I need to run 2 queries. Is it possible to do in 1 query?
2
Answers
No, you need to run two statements. That shouldn’t be a problem. If you are worried about race conditions, run the statements in a single transaction.
Per documentation Alter Table:
So to do it in one statement: