Within psql, redirect subsequent command output to a file. orodb=> \o /home/anton_faibyshev_sssd/ta.txt -- run your SELECTs ... orodb=> \o -- turn off redirection Notes: - \o without a path resets to stdout. - Ensure target path is writable.