Re: Adding a default value to a column after it exists

From: Harald Fuchs <hari(dot)fuchs(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Adding a default value to a column after it exists
Date: 2011-04-14 14:54:18
Message-ID: 86d3ko6fz9.fsf@mgm.protecting.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In article <20110413163120(dot)GU24471(at)shinkuro(dot)com>,
Andrew Sullivan <ajs(at)crankycanuck(dot)ca> writes:

> On Wed, Apr 13, 2011 at 09:21:20AM -0700, Gauthier, Dave wrote:
>> Is there a way to add a default value definition to an existing column? Something like an "alter table... alter column... default 'foo'".

> ALTER TABLE table ALTER [ COLUMN ] column SET DEFAULT expression

> (see http://www.postgresql.org/docs/9.0/interactive/sql-altertable.html)

> Note that this doesn't actually update the fields that are NULL in the
> column already. For that, once you had the default in place, you
> could do

> UPDATE table SET column = DEFAULT WHERE column IS NULL

And you probably want to do
ALTER TABLE table ALTER [ COLUMN ] column SET NOT NULL
after that.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dan Biagini 2011-04-14 15:10:47 pgsql 9.0.1 table corruption
Previous Message Lincoln Yeoh 2011-04-14 14:36:51 Re: SSDs with Postgresql?