2

Following is the query I'm using to scrub some fields in the JSONB column. I'm trying to radomize the first and last name so would like to use something like md5(random()::text) as values.

 update people set
 data = to_jsonb(data) || '{"firstName": "random_text", "lastName": "random_text"}'
 where id = 'b3c09005-7afb-4ad6-922d-76078875e59e';

I tried replacing random_text with md5(...) but I get an error

"DETAIL: Token "md5" is invalid.".

I also tried using || to concat but that didn't work either.

KazikM
1392 silver badges9 bronze badges
asked Dec 29, 2020 at 16:35

1 Answer 1

2

You have to use the md5 hash as a part of the string so you have to concat it

CREATE TABLE people("id" varchar(50), "data" jsonb,"firstName" text, "lastName" text)
 update people set
 data = to_jsonb(data) || ('{"firstName": "' || MD5('PostgreSQL MD5') || '", "lastName": "' || MD5('PostgreSQL MD5') || '"}')::jsonb
 where id = 'b3c09005-7afb-4ad6-922d-76078875e59e';

db<>fiddle here

still md5 is old and not very secure, so you should use a more secure version

answered Dec 29, 2020 at 18:02

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.