PostgreSQL: Function to reformat a string Suppose I have a string such as:

abcdefghi


and I would like to reformat it as:

abc def ghi


(that is, I have added some formatting spaces to the string).

Is there a PostgreSQL function which will do that? Something like:

reformat(abcdefghi,'xxx xxx xxx')


I know there isn’t the above function, but there might be a built in function to do something like that.

If not, I am happy to write such a function, but I though I would check.
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.