Home > OS >  Combine columns of text type to a jsonb column in postgresql
Combine columns of text type to a jsonb column in postgresql

Time:01-08

I have a table with below structure in postgres where id is the primary key.

┌──────────────────────────────────┬──────────────────┬───────────┬──────────┬──────────────────────────────────────────────────────────────┬──────────┬──────────────┬─────────────┐
│              Column              │       Type       │ Collation │ Nullable │                           Default                            │ Storage  │ Stats target │ Description │
├──────────────────────────────────┼──────────────────┼───────────┼──────────┼──────────────────────────────────────────────────────────────┼──────────┼──────────────┼─────────────┤
│ id                               │ bigint           │           │          │                                                              │ plain    │              │             │
│ requested_external_total_taxable │ bigint           │           │          │                                                              │ plain    │              │             │
│ requested_external_total_tax     │ bigint           │           │          │                                                              │ plain    │              │             │
│ store_address.country            │ text             │           │          │                                                              │ extended │              │             │
│ store_address.city               │ text             │           │          │                                                              │ extended │              │             │
│ store_address.postal_code        │ text             │

I want to convert the store_address fields to a jsonb column.

┌──────────────────────────────────┬──────────────────┬───────────┬──────────┬──────────────────────────────────────────────────────────────┬──────────┬──────────────┬─────────────┐
│              Column              │       Type       │ Collation │ Nullable │                           Default                            │ Storage  │ Stats target │ Description │
├──────────────────────────────────┼──────────────────┼───────────┼──────────┼──────────────────────────────────────────────────────────────┼──────────┼──────────────┼─────────────┤
│ id                               │ bigint           │           │          │                                                              │ plain    │              │             │
│ requested_external_total_taxable │ bigint           │           │          │                                                              │ plain    │              │             │
│ requested_external_total_tax     │ bigint           │           │          │                                                              │ plain    │              │             │
│ store_address                    │ jsonb            │           │          │                                                              │ extended │              │             │

Any efficient of doing this?

CodePudding user response:

You will need to add a new column, UPDATE the table and populating the new jsonb column. After that you can drop the old columns:

alter table the_table 
    add store_address jsonb;
    
update the_table
  set store_address = jsonb_build_object('country', "store_address.country", 
                                         'city', "store_address.city",
                                         'postal_code', "store_address.postal_code");
alter table the_table
  drop "store_address.country", 
  drop "store_address.city",  
  drop "store_address.postal_code"
  •  Tags:  
  • Related