9

I am trying to add a json type column to the table, but it doesn’t work and I cannot get normal examples, what am I doing wrong?

ALTER TABLE user ADD COLUMN purshased_product SET DATA TYPE JSONB USING purshased_product::JSONB;

I'm not trying to change the column, but just create a new one with json type

@Convert(converter = PurshasedProductConverter.class)
private PurshasedProductConverter[] purshasedProducts;

my variable

2 Answers2

22

To add a new column use:

ALTER TABLE "user" ADD COLUMN purshased_product jsonb;

Online example: https://rextester.com/SVST52826

The set data type and using clauses are only used to modify existing columns.


Note that useris a reserved keyword. It's a bad idea to create a table with that name. If you insist on that, you have to use double quotes each time you refer to the table (as I did)

  • Can not set [Lru.valyaeva.marathon.utils.PurshasedProductConverter; field ru.valyaeva.marathon.model.User.purshasedProducts to ru.valyaeva.marathon.model.PurshasedProduct – Самир Шахмурадлы Jan 22 '19 at 13:23
  • @СамирШахмурадлы: that is not a Postgres error message and most probably does not occur when you run the `ALTER TABLE` statement but somewhere in your application. –  Jan 22 '19 at 13:23
4

Sample:

ALTER TABLE schema.t_my_table
ADD COLUMN purshased_product jsonb; (You may also use the type json)

Please check here to understand the diff. between the two data types json and jsonb.

The data types json and jsonb, as defined by the PostgreSQL documentation,are almost identical; the key difference is that json data is stored as an exact copy of the JSON input text, whereas jsonb stores data in a decomposed binary form; that is, not as an ASCII/UTF-8 string, but as binary code.

Vinto
  • 356
  • 3
  • 7