psql -o out.txt -c 'SELECT * FROM users;'

Category: Databases & SQL

psql -o out.txt -c 'SELECT * FROM users;'

Write query output to a file

The -o flag sends query results to out.txt instead of the terminal, handy in scripts. Combine with -A -t for clean machine-readable files. Related: \o inside psql does the same.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.