Copying Postgres output into a spreadsheet

I often need to grab information from a Postgres database and paste it into a spreadsheet for sharing with others. Google Sheets needs the pasted data to be tab-separated in order to be correctly split into columns. This isn’t the default behaviour for psql but here’s how to configure psql’s output to get it.

At a psql prompt, switch to unaligned output

=> \a

set the field separator to a tab character:

=> \f '\t'

and turn the pager off:

=> \pset pager off

then the output from subsequent SELECT ... statements can be cleanly pasted into your Google Doc.

——————

Something wrong? Suggest an improvement or add a comment (see article history)
Tagged with: postgres
Filed in: tips

Previous: An SSH tip for modern AWS patrons
Next: A useful template for commit messages

Copyright © 2005-2024 David Winterbottom
Content licensed under CC BY-NC-SA 4.0.