Interview Q&A / PostgreSQL

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How do you update a single key inside a JSONB column without replacing the whole document?

SHORT ANSWER

Use jsonb_set(target, path, new_value) or the || concatenation operator to produce a modified copy, and assign it in an UPDATE. PostgreSQL still rewrites the entire jsonb value, since there is no in-place edit.

Explain it simply

A JSONB value is stored as one unit, like a laminated card. To change one line you make a corrected copy of the full card and swap it in. The functions below build that corrected copy for you so you only describe the change.

Example

sql
UPDATE users
SET profile = jsonb_set(profile, '{address,city}', '"Mumbai"')
WHERE id = 1;

-- add or overwrite top-level keys
UPDATE users
SET profile = profile || '{"verified": true, "age": 23}'
WHERE id = 1;

-- remove a key
UPDATE users SET profile = profile - 'age' WHERE id = 1;

-- remove a nested key by path
UPDATE users SET profile = profile #- '{address,city}' WHERE id = 1;

Note the new value for jsonb_set must itself be valid JSON, so a string needs inner double quotes: '"Mumbai"'. By default, jsonb_set creates the last path key if missing (the create_if_missing argument is true), but it will not create missing intermediate levels.

Because the whole value is rewritten, very large documents that change often are better split into columns or tables.

What to say in the interview

"jsonb_set changes a value at a path, || merges keys, - and #- delete. The result is a new jsonb that replaces the old one, so large frequently-updated documents are a sign I should normalise into columns."

Common mistakes

  • Passing a bare string like 'Mumbai' instead of '"Mumbai"' to jsonb_set.

#postgresql#jsonb#update

Related questions

Practise more PostgreSQL questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open PostgreSQL quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.