title: Postgres: create index on attribute of attribute in JSONB column?
body: <p>I'm working in Postgres 9.6.5. I have the following table:</p>
  
  <pre><code> id             | integer      
   data           | jsonb 
  </code></pre>
  
  <p>The data in the <code>data</code> column is nested, in the form:</p>
  
  <pre><code>{ 'identification': { 'registration_number': 'foo' }}
  </code></pre>
  
  <p>I'd like to index <code>registration_number</code>, so I can query on it. I've tried this (based on <a href="https://stackoverflow.com/questions/17807030/how-to-create-index-on-json-field-in-postgres-9-3">this answer</a>):</p>
  
  <pre><code>CREATE INDEX ON mytable((data-&gt;&gt;'identification'-&gt;&gt;'registration_number'));
  </code></pre>
  
  <p>But got this:</p>
  
  <pre><code>ERROR:  operator does not exist: text -&gt;&gt; unknown
  LINE 1: CREATE INDEX ON psc((data-&gt;&gt;'identification'-&gt;&gt;'registration...                                                    ^
  HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
  </code></pre>
  
  <p>What am I doing wrong?</p>