For Windows:
Connect to the databse:
cmd
psql -d db_name -U postgres
Copy the file in hex format as Grzegorz Szpetkowski showed
psql
\copy (SELECT encode(file, 'hex') FROM files WHERE id = 1) TO 'D:\file.hex'
Convert to the desired format
cmd
cd /d d:
certutil -decodehex file.hex file.jpg