Well, as I mentioned you can connect to one of the Pg system tables if you have access to Pg at all. If you don't, you may actually be able to do something like this (note no DBI or connection):
use DBD::Pg;
my $foo = q{foo'bar'baz};
print DBD::Pg::db->quote($foo);
# prints 'foo''bar''bas'