How to append datetime in filename in postgresql 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.
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;"