Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - ALTER column data type from integer to integer array

Kindly help to modify a column of type integer to integer array:

I had created a table with a column content_id of type integer. then I tried to change the content_id(integer) to integer[](integer array) but its showing error as displayed:

TestDatabase=# ALTER TABLE tbl_handset_content ALTER COLUMN content_id TYPE integer[];
ERROR:  column "content_id" cannot be cast to type "pg_catalog.int4[]"

Regards,

Sravan

like image 668
srchalikonda Avatar asked Dec 14 '12 13:12

srchalikonda


People also ask

How do I change the datatype of a column in PostgreSQL?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

Which one is correct altering a column in PostgreSQL?

The syntax to modify a column in a table in PostgreSQL (using the ALTER TABLE statement) is: ALTER TABLE table_name ALTER COLUMN column_name TYPE column_definition; table_name. The name of the table to modify.

How can I change PostgreSQL type?

You must own the type to use ALTER TYPE . To change the schema of a type, you must also have CREATE privilege on the new schema. To alter the owner, you must also be a direct or indirect member of the new owning role, and that role must have CREATE privilege on the type's schema.

How do I change data to numeric in PostgreSQL?

Use the :: operator to convert strings containing numeric values to the DECIMAL data type. In our example, we converted the string ' 5800.79 ' to 5800.79 (a DECIMAL value). This operator is used to convert between different data types. It's very popular within PostgreSQL.


1 Answers

Try this (column test_id is of type INTEGER before alter takes place). PostgreSQL 8.4.

ALTER TABLE test.test_id
    ALTER COLUMN test_id TYPE INTEGER[]
    USING array[test_id]::INTEGER[];
like image 92
Wojtas Avatar answered Sep 30 '22 18:09

Wojtas