Postgresql – How to change the datatype of a column from integer to money

alter-tabledatatypesmoneypostgresql

I am attempting to convert a PostgreSQL table column from integer to money, but I am receiving the error:

cannot cast type MyColumn to money

I have tried these two statements, but just haven't found any real example on how to really do it.

ALTER TABLE products 
    ALTER COLUMN price TYPE money

and:

ALTER TABLE products 
    ALTER COLUMN price TYPE money USING to_char(price, '999.99')

How can you change the datatype of a PostgreSQL column from integer to money?

Best Answer

Just try the following command.

ALTER TABLE products ALTER COLUMN price TYPE money using price::text::money

Non-quoted numeric values can be converted to money by casting the numeric value to text and then money