Wednesday, April 1, 2009

Type Casting column type in Postgres

Consider you have a filled up table in postgres, and one of the column is defined as 'character varying'. Now, if you want to change the column type to 'integer' then the normal alter command will not work.

If you will do this :

alter table tablename alter columnname type integer

you will get this error:

ERROR: column "column_name" cannot be cast to type "pg_catalog.int4"

So, you need to do this:

alter TABLE tablename alter column columnname type integer using columnname::integer;

No comments:

Post a Comment