in reply to Re^2: processing PSQL with system command
in thread processing PSQL with system command

Maybe slightly long in the tooth, but Yes, even you can use CPAN

Edit: That being said, if you're dead set on taking the hard route you'd be better served writing the SQL into a file and then telling psql to run that SQL script instead of trying using -c and outguessing how n layers of arbitrary quoting are going to be pulled (or not pulled) apart by however many levels of interpreting.

The cake is a lie.
The cake is a lie.
The cake is a lie.