3

I'm trying to change the datatype of a column to varchar but get the following error

ERROR:  syntax error at or near "type" at character 40

My code looks as follows

alter table n_logs alter column action type varchar(100);

I'm running PostgreSQL 7.4.13 (Yeah, I know I need to upgrade)

2 Answers 2

8

I don't think you can do that: http://www.postgresql.org/docs/7.4/interactive/ddl-alter.html

You should split it into 3 steps

  1. add new column
  2. copy values from 1st column to 2nd
  3. drop old column
Sign up to request clarification or add additional context in comments.

4 Comments

I would add 4. rename new column to old column
@andersonbd1 Adding a new column is only at the END of a table. Can't we put the column in the original place like with mysql?
@itsols - not sure... just curious - why is the order important to you?
@andersonbd1 When we work in teams AND even as a single programmer, I think having the fields in a particular order contribute towards self-documentation AND helps prevent mistakes/oversights to some degree. For example, if you see a table with 30 fields like ID, Name, Address, ..... and PHONE at the end, this can be misleading. Also someone may take for granted that there's no phone in it. Also we waste time looking through all fields to see if a particular field is in or not. BTW I Found a method to do this. I have another post with an answer. So it IS possible :)
3

I know you said you already found a solution, but your command would have worked if you took out the "action" keyword, just like this:

ALTER TABLE table_name_here 
ALTER COLUMN column_name_here type varchar(100); 

The SQL above worked for me, thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.