alter varchar() column length?

Started by Gauthier, Daveabout 18 years ago3 messagesgeneral
Jump to latest
#1Gauthier, Dave
dave.gauthier@intel.com

Is there a way to alter a varchar column's length? Maybe something like
"alter table foo alter column xyz varchar(256)".

My alternative seems very messy...

- alter table, add a new column with the desired varchar
length. call it "temp"

- insert into "temp" the value of the old column.

- alter table, drop the old column (messing up all the indexes
on that column in the process I suppose)

- alter table, add a new column with the same name as the one
you just deleted, only with the desired length

- insert into that new column the value in "temp"

- alter table, drop "temp"

- recreate all the indexes

Thanks

-dave

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Gauthier, Dave (#1)
Re: alter varchar() column length?

Hello

CREATE TABLE foog(a varchar(10));
ALTER TABLE foog ALTER COLUMN a TYPE varchar(30);

postgres=# \d foog
Table "public.foog"
Column | Type | Modifiers
--------+-----------------------+-----------
a | character varying(30) |

regards
Pavel Stehule

Show quoted text

On 11/01/2008, Gauthier, Dave <dave.gauthier@intel.com> wrote:

Is there a way to alter a varchar column's length? Maybe something like
"alter table foo alter column xyz varchar(256)".

My alternative seems very messy...

- alter table, add a new column with the desired varchar length.
call it "temp"

- insert into "temp" the value of the old column.

- alter table, drop the old column (messing up all the indexes on
that column in the process I suppose)

- alter table, add a new column with the same name as the one you
just deleted, only with the desired length

- insert into that new column the value in "temp"

- alter table, drop "temp"

- recreate all the indexes

Thanks

-dave

#3Thomas Kellerer
spam_eater@gmx.net
In reply to: Gauthier, Dave (#1)
Re: alter varchar() column length?

Gauthier, Dave, 11.01.2008 15:55:

Is there a way to alter a varchar column�s length? Maybe something like
�alter table foo alter column xyz varchar(256)�.

A quick look into the manual would have revealed the correct syntax:

http://www.postgresql.org/docs/8.2/static/sql-altertable.html