How to insert a created_date and updated_date in JSON object in JSONB column in PostgreSQL

荒凉一梦 提交于 2019-12-11 23:30:02

问题


{"request": 12, "createdby": "sam"} is the JSON data to be inserted in a PostgreSQL table with two columns request id int, data JSONB.

The Jain data can be inserted into data column with JSONB datatype. can we append created_date in the JSON object before inserting it into the data column.


回答1:


sure, you can just concat jsonb with || operator

t=# create table so9(rid int, d jsonb);
CREATE TABLE
t=# insert into so9 (d) select '{"request": 12, "createdby": "sam"}'::jsonb || concat('{"created_date":"',now(),'"}')::jsonb;
INSERT 0 1
t=# select * from so9;
 rid |                                          d
-----+--------------------------------------------------------------------------------------
     | {"request": 12, "createdby": "sam", "created_date": "2018-03-30 17:24:05.246852+01"}
(1 row)


来源:https://stackoverflow.com/questions/49644090/how-to-insert-a-created-date-and-updated-date-in-json-object-in-jsonb-column-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!