How to append datetime in filename in postgresql

Posted on

Question :

How to append datetime in filename in below command:

COPY (select  query) To '/tmp/test.csv' DELIMITER ',' CSV HEADER;  

I want my filename like test_date_tim.csv.

Answer :

If using psql via bash command line, you can simply do:

psql -c "COPY (select  query) To '/tmp/test_`date +"%Y-%m-%d_%H%M%S"`.csv' DELIMITER ',' CSV HEADER;"

Leave a Reply

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