Skip to content
Advertisement

Postgres: Update jsonb from other table column which is encrypted

I am migrating a data from an encrypted jsonb column (app_id field) in a table to another jsonb column in a different table.

As said: origin table column is encrypted. Destination one is not.

Right now I have the following query:

UPDATE destination d 
SET payload = jsonb_set(payload,'{app_id}', to_jsonb('the_origin.app_id'), true)
FROM 
    (SELECT array_to_string(array_agg(PGP_SYM_DECRYPT(d.payload::bytea, 'the-decrypt-pass')::json->>'app_id'::text), ',') as app_id 
        FROM origin 
        WHERE id='origin_id'
    ) as the_origin
WHERE d.id='destination_id';

Error response is:

SQL Error [42804]: ERROR: could not determine polymorphic type because input has type unknown

From here I have used the array_to_string(array_agg “hack”, but this is still not working.

EDIT: as suspected it is not related (yet) to the encrypt column, the following query with a non encrypted value gets the same error response

UPDATE destination d 
SET payload = jsonb_set(payload,'{o.value}', to_jsonb('origin.destination_value'), true)
FROM origin o
WHERE d.origin_id=o.id;

Advertisement

Answer

From previous link and from this question: How to update JSONB column with value coming from another table column in PostgreSQL . Following query is fully working:

UPDATE destination d 
SET payload = payload || jsonb_build_object('app_id', the_origin.app_id)
FROM 
    (SELECT array_to_string(array_agg(PGP_SYM_DECRYPT(d.payload::bytea, 'the-decrypt-pass')::json->>'app_id'::text), ',') as app_id 
        FROM origin 
        WHERE id='origin_id'
    ) as the_origin
WHERE d.id='destination_id';
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement