title: Unicode normalization in Postgres
body: <p>I have a large number of Scottish and Welsh accented place names (combining grave, acute, circumflex and diareses) which I need to update to their unicode normalized form, eg, the shorter form 00E1 (\xe1) for <code>á</code> instead of 0061 + 0301 (\x61\x301)</p>
  
  <p>I have found a solution from an old Postgres nabble mail list from 2009, using pl/python,</p>
  
  <pre><code>create or replace function unicode_normalize(str text) returns text as $$
    import unicodedata
    return unicodedata.normalize('NFC', str.decode('UTF-8'))
  $$ LANGUAGE PLPYTHONU;
  </code></pre>
  
  <p>This works, as expected, but made me wonder if there was any way of doing it directly with built-in Postgres functions. I tried various conversions using convert_to, all in vain.</p>
  
  <p>EDIT: As Craig has pointed out, and one of the things I tried:</p>
  
  <pre><code>SELECT convert_to(E'\u00E1', 'iso-8859-1');
  </code></pre>
  
  <p>returns <code>\xe1</code>, whereas</p>
  
  <pre><code>SELECT convert_to(E'\u0061\u0301', 'iso-8859-1');
  </code></pre>
  
  <p>fails with the <code>ERROR:  character 0xcc81 of encoding "UTF8" has no equivalent in "LATIN1"
  </code></p>