PostgreSQL: Function to reformat a string
Use regexp_replace

SELECT regexp_replace('abcdefghiabcdefghi', '(.{3})', '\1 ', 'g');
      regexp_replace      
--------------------------
 abc def ghi abc def ghi 
(1 row)


This inserts a space every three characters.