Adding a default value to a column after it exists

From: "Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Adding a default value to a column after it exists
Date: 2011-04-13 16:21:20
Message-ID: 482E80323A35A54498B8B70FF2B87980048E2E6578@azsmsx504.amr.corp.intel.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Is there a way to add a default value definition to an existing column? Something like an "alter table... alter column... default 'foo'".

I thought of a clumsy way to do this... create a temp column, set it's value to that of the column to be altered, drop the column to be altered, redefine it with the default, shunt all the values in the temp column over to the new column and then drop the temp column. But I have before and after triggers on the table that react to changes in this column (not to mention the need for it's existence).

I could add something to the before trigger to do this too. But it would be cleaner to do this as a column property.

Thanks for any help.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Mike Fowler 2011-04-13 16:25:05 Re: Adding a default value to a column after it exists
Previous Message Andy Chambers 2011-04-13 15:23:35 Cursor metadata