I have a special case requiring that I generate part of a SQL WHERE clause from user supplied input values. I want to prevent any sort of SQL Injection vulnerability.
The most easiest way would be to use PostgreSQL's Dollar Quoting in the combination with a small random tag:
Otherwise build your query like this:
$tag$inputString$tag$
This way you escape the whole hassle of different nested quoting techniques and you also set up a moving target by using a random tag.
Depending on your security requirements this might do the job or not. :-)