The following is a snippet of a table called “containers”.
Column | Type | Modifiers
--------------------+-----------------------------+---------------------------------
id | uuid | not null
name | character varying(255) |
products | character varying | default '{}'::character varying
How can I alter the products column to "character varying[]" and the corresponding modifiers to default '{}'::character varying[] ? Essentially, I want to convert a string to a string array. Note the products column has no limit on the number of characters.
alter table "containers" alter "products" type character varying[];
throws the following error
ERROR: column “products” cannot be cast to type character varying[]
There is no implicit cast from
varchartovarchar[]in Postgres.You must indicate how to perform the conversion of the types.
You should do it in
USING expressionclause (see ALTER TABLE in the documentation).In that case you have to drop and recreate the default value of the column, as it is explained in the documentation:
The three operations can be done in one statement: