Re: Adding a New Column Specifically In a Table

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Adding a New Column Specifically In a Table
Date: 2010-10-14 14:56:15
Message-ID: 4CB71A0F.6060908@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 10/14/2010 04:32 AM, Thom Brown wrote:
> On 13 October 2010 23:19, Raymond O'Donnell <rod(at)iol(dot)ie> wrote:
>> On 13/10/2010 19:04, Carlos Mennens wrote:
>>>
>>> OK so I have read the docs and Google to try and find a way to add a
>>> new column to an existing table. My problem is I need this new column
>>> to be created 3rd rather than just dumping this new column to the end
>>> of my table. I can't find anywhere how I can insert my new column as
>>> the 3rd table column rather than the last (seventh). Does anyone know
>>> how I can accomplish this or if it's even possible. Seems like a
>>> common task but I checked the documentation and may have missed it in
>>> my reading.
>>>
>>
>> It isn't possible at the moment. This has come up a good bit in the past, so
>> you'll find debate in the archives...
>>
>> One work-around is to add the column, and then do:
>>
>> create table new_table as
>> select [columns in desired order] from old_table;
>>
>> drop table old_table;
>>
>> alter table new_table rename to old_table;
>>
>> ...remembering to deal with foreign key constraints as you go.
>
> ..and indexes, triggers, rules, views and every other dependency.
> It's a potentially tricky solution to something which shouldn't really
> be a problem.
>
True indeed. I suspect OP is stuck using some (crud-ie?) "platform"
which is automagically producing the presentation so this approach
(drop,recreate all involved) is likely to be the best bet. My guess is
the number of users isn't huge (still setting schema, not adding
employer_id, etc) so this approach would be a pretty quick and testable fix.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Thom Brown 2010-10-14 15:35:03 Re: How to remove "enter" or new line
Previous Message Reuven M. Lerner 2010-10-14 14:12:28 Re: Passing refcursors between pl/pgsql functions