title: Is it possible to store invalid UTF-8 byte sequences in a postgres text column?
body: <p>The encoding of my postgres database is UTF-8. In a certain table I have a <a href="https://www.postgresql.org/docs/current/static/datatype-character.html" rel="nofollow noreferrer"><code>text</code></a> column into which I would like to insert some data. Now, the data is <em>mostly</em> valid UTF-8, but there are a number of instances of invalid byte sequences which I do <em>not</em> want to remove or substitute. My question is, is there any way of inserting the data into the <code>text</code> column without removing or substituting its invalid byte sequences?</p>
  
  <p>Here's a simple example, executed from the shell (bash) command-line courtesy of <a href="https://www.postgresql.org/docs/current/static/app-psql.html" rel="nofollow noreferrer"><code>psql</code></a>:</p>
  
  <pre><code>psql main postgres &lt;&lt;&lt;"create table t1 (a text); insert into t1 (a) values (E'a\xC0b');";
  ## CREATE TABLE
  ## ERROR:  invalid byte sequence for encoding "UTF8": 0xc0 0x62
  </code></pre>
  
  <p>I know this is probably a long shot, but is there any way of disabling postgres's validation of inserted text, perhaps on an ad hoc basis? I don't see how it would trouble postgres to have some byte sequences in <code>text</code> column data that happen to not be valid for the database's configured character encoding.</p>
  
  <p>If this is not possible, I guess the only recourse is to store the data as straight binary data using the <a href="https://www.postgresql.org/docs/current/static/datatype-binary.html" rel="nofollow noreferrer"><code>bytea</code></a> data type, but please let me know if there's a better solution out there.</p>