Only difference in your case is you would output to a file instead ofr browser stream.

You can also do it with plpython, plperl, or any other postgresql pl language that can write to a file if you just want to output to the database server. We'l try to put together examples of that in the next month or so.