Skip to content

Instantly share code, notes, and snippets.

@carlosayam
Created May 16, 2018 21:07
Show Gist options
  • Save carlosayam/f75f51c71cd11564f06971864c9cdcef to your computer and use it in GitHub Desktop.
Save carlosayam/f75f51c71cd11564f06971864c9cdcef to your computer and use it in GitHub Desktop.
best way to get csv output from postgresql in psql via \i example.sql
\set somevar1 '\'2018-01-01\''
\set somevar2 '\'2017-01-01\''
\echo Starting
\pset fieldsep ','
\pset format unaligned
\pset null '(null)'
\o 'your-output.csv'
-- here your query
select
field1, field2
from a_table;
\o
\echo finished
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment