Extract all of the keys, including nested, from postgres JSONB column
You can use a recursive query for that:

with recursive all_keys (ky, nested) as (
  select t.ky, 
         case jsonb_typeof(p.data -&gt; t.ky) 
           when 'object' then p.data -&gt; t.ky
           else null
         end
  from profiles p
    cross join jsonb_object_keys(p.data) as t(ky)
  where id = 5 
  union all
  select t.ky,
         case jsonb_typeof(ak.nested -&gt; t.ky) 
           when 'object' then ak.nested -&gt; t.ky
           else null
         end
  from all_keys ak
    cross join jsonb_object_keys(ak.nested) as t(ky)
  where nested is not null
)
select ky
from all_keys;


Online example: https://rextester.com/YVNF32970