Home > Mobile >  How to convert a column of type string to jsonb
How to convert a column of type string to jsonb

Time:01-27

Sorry for my English. I have a table in my database that has a table of type string. I need to convert the type of that column to jsonb.

I need to convert this:

--------------------
table_a
--------------------
id | type (string)
--------------------
1  | aaa
2  | bbb
--------------------

for this

--------------------
table_a
--------------------
id | type (jsonb)
--------------------
1  | ["aaa"]
2  | ["bbb"]
--------------------

I need a sql script to do this. I've tried to create scripts, but to no avail!

can anybody help me?

CodePudding user response:

You can ALTER the type to be jsonb while converting the values in the same statement

alter table the_table
    alter "type" type jsonb
    using  jsonb_build_array ("type");
  • Related