title: Inserting text string with hex into PostgreSQL as a bytea
body: <p>I have a text file with several strings of hex in it:</p>
  
  <pre><code>013d7d16d7ad4fefb61bd95b765c8ceb
  007687fc64b746569616414b78c81ef1
  </code></pre>
  
  <p>I would like to store these in the database as a <em>bytea</em>, instead of a <em>varchar</em>. That is, I would like the database to store 01 as the single byte 00000001, not characters '0' &amp; '1'.</p>
  
  <p>I can easily run this file through sed to format/escape it any way I need to.</p>
  
  <p>This is what I have tried:</p>
  
  <pre><code>create table mytable (testcol BYTEA);
  </code></pre>
  
  <p>This works:</p>
  
  <pre><code>insert into mytable (testcol) values (E'\x7f\x7f');
  </code></pre>
  
  <p>However, as soon as I have a byte that goes above \x7f, I get this error:</p>
  
  <pre><code>insert into mytable (testcol) values (E'\x7f\x80');
  ERROR:  invalid byte sequence for encoding "UTF8": 0x80
  </code></pre>
  
  <p>Any ideas, or am I approaching things wrong?</p>