If you do not mind substituting all the space characters, regardless of whether they are kept / ending, then do the following:
SELECT REPLACE(REPLACE(REPLACE(REPLACE(txt, ' ', '_'), E'\t', '\t'), E'\r', '\r'), E'\n', '\n') AS txt FROM test;
This underline emphasizes spaces, but of course you can choose your own. See the SQL script demo .
If you strictly want to display leading / trailing, it will be more complex, but if it is really necessary, something is possible with regex_replace .
source share