Skip to content

Instantly share code, notes, and snippets.

@samber
Last active October 20, 2020 08:59
Show Gist options
  • Save samber/a72417c1096981900ab919f902a8405a to your computer and use it in GitHub Desktop.
Save samber/a72417c1096981900ab919f902a8405a to your computer and use it in GitHub Desktop.
Postgresql query to Google Spreadsheet

Export Postgresql data to Spreadsheet

Dependencies

psql + jq

Export from PG

export SQL_QUERY="SELECT firstname, lastname, email FROM users"

echo "SELECT row_to_json(x.*) FROM (${SQL_QUERY}) x;" | psql postgresql://user:mdp@hostname:5432/dbname -t -- | jq -r 'to_entries|map(.value)|@csv'

Import into Spreadsheet

1- Copy/paste results to a new spreadsheet

2- Select imported cells

3- Menu Data > Split text to columns

4- Add a line on top, with column description

5- Select the first line (column descriptions)

6- Menu Data > Create a filter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment