Skip to content
Advertisement

Postgres – Add a column to a table with a default value, but set existing rows to a different default value

I want to add a boolean column to a table and have the default value be false for newly created rows, but all existing rows should be set to true. How can I do that?

Advertisement

Answer

First alter table and add column as alter table table_name add column column_name boolean default false;

Then update value of that column as

update table_name set column_name=true;

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement