title: Proper way to convert bytea from Postgres back to a string in python
body: <p>I have a small script where I'm generating SHA1 hashes, taking the binary representation through hashlib's hexdigest, and storing them in a Postgres DB with a bytea column. I have a query that looks like this in Postgres (abbreviated):</p>
  
  <pre><code>SELECT * FROM some_table WHERE some_hash in decode(another_hash, 'hex')
  </code></pre>
  
  <p>When executing a query, I have some code that looks like this:</p>
  
  <pre><code>cur.execute(query)
  for hash_rep in cur:
      print bhash
  </code></pre>
  
  <p>Now, in that print statement, it will either print out unintelligible characters, or if I change it to:</p>
  
  <pre><code>print str(psycopg2.Binary(bhash))
  </code></pre>
  
  <p>I get something like:</p>
  
  <pre><code>'\214p\203\030\017K\247\373o\253'::bytea
  </code></pre>
  
  <p>What is the correct way to convert it back to the original string? The original representations are something like "30d22d5d64efe4c5333e", and I'd like to get it back to that original string for comparison purposes. I'm not sure if I'm missing something obvious, </p>