Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a column from string to string array in postgresql

Tags:

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[]

like image 368
paddle42380 Avatar asked Feb 08 '13 22:02

paddle42380


People also ask

How do I change the column type 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.

How do I declare a string array in PostgreSQL?

Syntax. In the above syntax, we can declare a String Array data type at the time of table creation. Where table name is the specified table name that we need to create and column 1, column 2, and column n declared with the data type of array and it separated by using a comma.

How do I declare an array variable in PostgreSQL?

PostgreSQL Array type PL/pgSQL in PostgreSQL allows us to declare a variable as an ARRAY type. This ARRAY can be either a base or a custom type. For example, if we want to store a list of PINCODE values, then, we can declare the variable as v_pincode INT[].


1 Answers

There is no implicit cast from varchar to varchar[] in Postgres. You must indicate how to perform the conversion of the types. You should do it in USING expression clause (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 USING option of SET DATA TYPE can actually specify any expression involving the old values of the row; that is, it can refer to other columns as well as the one being converted. This allows very general conversions to be done with the SET DATA TYPE syntax. Because of this flexibility, the USING expression is not applied to the column's default value (if any); the result might not be a constant expression as required for a default. This means that when there is no implicit or assignment cast from old to new type, SET DATA TYPE might fail to convert the default even though a USING clause is supplied. In such cases, drop the default with DROP DEFAULT, perform the ALTER TYPE, and then use SET DEFAULT to add a suitable new default.

alter table containers alter products drop default;
alter table containers alter products type text[] using array[products];
alter table containers alter products set default '{}';

The three operations can be done in one statement:

alter table containers 
    alter products drop default,
    alter products type text[] using array[products],
    alter products set default '{}';
like image 74
klin Avatar answered Sep 18 '22 19:09

klin