title: PostgreSql: How to query using encrypted columns
body: <p><strong>SCENARIO:</strong></p>
  <p>I have created a table:</p>
  <pre><code>Create table knights(age integer, nickname varchar(255));
  </code></pre>
  <p>Then I inserted a few records and encrypted the nickname column data using pgcrypto:</p>
  <pre><code>insert into knights values(21, PGP_SYM_ENCRYPT('ShiningArmor','AES_KEY')::varchar);
  insert into knights values(32, PGP_SYM_ENCRYPT('Rigid','AES_KEY')::varchar);
  </code></pre>
  <p><strong>PROBLEM:</strong>
  Now I try to get records from the table using the encrypted nickname column as per suggested in <a href="https://stackoverflow.com/questions/7753375/can-postgresql-do-a-query-on-encrypted-records">this answer</a>:</p>
  <pre><code>SELECT * FROM knights WHERE nickname = pgp_sym_encrypt('Rigid', 'AES_KEY')::varchar;
  </code></pre>
  <p>I get nothing back. Notice that I had to cast the <code>nickname</code> to <code>varchar</code>. Even if I change the column type to bytea, I am still getting nothing. Please note that my symmetric key is actually the same: <code>AES_KEY</code>. I did not generate it anywhere. Do I need to change the length?</p>
  <p>My PostGreSql version is 9.6.</p>