I have a table
CREATE TABLE policyholder(policyholder_id text, fname text, lname text, risk_score text, high_risk text);
INSERT INTO policyholder VALUES
('PH3068','ADAM','OCHSENBEIN','88','1'),
('PH3069','MALINDA','MEHSERLE','42','0'),
('PH3070','SANDRA','KUHTA','20','0');
and I want to get a table AS
CREATE TABLE mod(id text, others text);
INSERT INTO mod
SELECT policyholder_id, row_to_json(row(fname, lname, risk_score, high_risk)) from policyholder;
i.e., one column as id, and others as json.
However, we have
policyholder_id | row_to_json
-----------------+-----------------------------------------------------
PH3068 | {"f1":"ADAM","f2":"OCHSENBEIN","f3":"88","f4":"1"}
PH3069 | {"f1":"MALINDA","f2":"MEHSERLE","f3":"42","f4":"0"}
PH3070 | {"f1":"SANDRA","f2":"KUHTA","f3":"20","f4":"0"}
bcuz the ROW()
drops column name.
I don’t want to create a new type because there is too much of this function.
I notice a similar question How to create a row without losing the column names, for input of row_to_json?
However, I failed to figure out how to apply it to this situation…
2
Answers
The first version below is a partial solution.
The second version matches your desired result.
Here is a version where I remove the
policyholder_id
value from the JSON. This seems to require casting asjsonb
:Insert Into using Select does not change the column name.
The above command is ok. If you want to change the definition of a table use DDL commands (Create, Alter …). Insert, Select, Update will never change the table difinition.
However, if you run just the Select command…
… then the second column will have label row_to_json because you didn’t name the column… Use alias like below
See the fiddle here.