I have a need to change the length of CHAR columns in tables in a PostgreSQL v7.4 database. This version did not support the ability to directly change the column type or size using the ALTER TABLE statement. So, directly altering a column from a CHAR(10) to CHAR(20) for instance isn't possible (yeah, I know, "use varchars", but that's not an option in my current circumstance). Anyone have any advice/tricks on how to best accomplish this? My initial thoughts:
-- Save the table's data in a new "save" table. CREATE TABLE save_data AS SELECT * FROM table_to_change;
-- Drop the columns from the first column to be changed on down. ALTER TABLE table_to_change DROP column_name1; -- for each column starting with the first one that needs to be modified ALTER TABLE table_to_change DROP column_name2; ...
-- Add the columns back, using the new size for the CHAR column ALTER TABLE table_to_change ADD column_name1 CHAR(new_size); -- for each column dropped above ALTER TABLE table_to_change ADD column_name2...
-- Copy the data bace from the "save" table UPDATE table_to_change SET column_name1=save_data.column_name1, -- for each column dropped/readded above column_name2=save_date.column_name2, ... FROM save_data WHERE table_to_change.primary_key=save_data.primay_key;
Yuck! Hopefully there's a better way? Any suggestions appreciated. Thanks!