Postgres, copy to a csv file [closed]

Posted on

Question :

On Postgres, I want to load a select query into a csv file. But in the CSV file I want to put some values, that doesn’t appear in the query, how do I do that? It’s like:

select id, name, email  

And then have a Date, which I want to set default in every line. So the csv will be like:

id,name,email,date

Answer :

Just select the additional value:

select id, name, email, current_date as "date"
from the_table;

or

select id, name, email, date '2018-10-09' as "date"
from the_table;

You can COPY or copy from an arbitrary query if you put parentheses around it.

copy (select id, name, email, now() as "date" from example) to file.csv with (format csv)

Leave a Reply

Your email address will not be published. Required fields are marked *