If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > "ALTER COLUMN ... TYPE" problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-28-04, 17:30
The-Saint The-Saint is offline
Registered User
 
Join Date: Jan 2004
Posts: 30
Question "ALTER COLUMN ... TYPE" problem

table user_general has a field named user_name which is varchar(40) now. I want to make it varchar(15). Here is SQL I use:

ALTER TABLE user_general
ALTER COLUMN user_name TYPE varchar(15)

it gives me error:

SQL error:
ERROR: syntax error at or near "TYPE" at character 50
In statement:
ALTER TABLE user_general
ALTER COLUMN user_name TYPE varchar(15)
Reply With Quote
  #2 (permalink)  
Old 10-28-04, 22:06
zalew zalew is offline
Registered User
 
Join Date: Oct 2004
Posts: 3
hi. in postgres you can't so easily change column type.. the documentation of 'alter table' there's no word about changing column type, as if they didn't want to talk about it
for a long time i thought it was only possible by recreating the table, but i found something like this in the faq
Quote:
To change the data type of a column, do this:

BEGIN;
ALTER TABLE tab ADD COLUMN new_col new_data_type;
UPDATE tab SET new_col = CAST(old_col AS new_data_type);
ALTER TABLE tab DROP COLUMN old_col;
COMMIT;

You might then want to do VACUUM FULL tab to reclaim the disk space used by the expired rows.
Reply With Quote
  #3 (permalink)  
Old 11-25-04, 01:27
danie danie is offline
Registered User
 
Join Date: Nov 2004
Posts: 1
changing column type

Quote:
Originally Posted by zalew
hi. in postgres you can't so easily change column type.. the documentation of 'alter table' there's no word about changing column type, as if they didn't want to talk about it
for a long time i thought it was only possible by recreating the table, but i found something like this in the faq
This works well, but remeber to add a line to rename new_col to old_col.
The new col now renamed to old_col will appear at the end of the list of fields in the table definition.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On