The following two lines have been lifesavers for the situation where a picture is properly inserted into the PostgreSQL, but cannot be taken out and displayed, even if the headers and whatever else needed is properly in place.
pg_query($connection, 'SET client_encoding="UTF8"');
pg_query($connection, "SET bytea_output='escape'");
Code language: PHP (php)
Hope this helps somebody out there!