In the simplified canonical example (which I often see on forums and in the books) of a raster exporting script the OID (returned by lo_create(0), here is 9585208) has to be known before the penultimate script line (lo_export 9585208 'C:/temp/raster.png'):
SELECT oid, lowrite(lo_open(oid, 131072), img) As num_bytes
FROM (
VALUES (
lo_create(0),
(SELECT ST_AsPNG(rast)
FROM bag_o_rasters
LIMIT 1)
)
) As v(oid, img);
lo_export 9585208 'C:/temp/raster.png'
SELECT lo_unlink(9585208);
I have a hard time with figuring out how to make PSQL cli utility to run this script in one pass, i.e., how to communicate OID returned by lo_create(0) to lo_export command. It seems the lo_export is client side command and it is not able to digest any query results... please, help...