postgresql - Postgres Query copy to CSV with DateTime -




i have simple query run inside postgres sql query runs bunch of queries extracts csv, [without bunch of queries above it] is

copy (select * public.view_report_teamlist_usertable_wash)     'd:/sf/reports/view_report_teamlist_usertable_wash.csv'     delimiter ','    csv header encoding 'utf8' force quote *; 

can alter above @ append date/time [now()] filename? ie. 'd:/sf/reports/view_report_teamlist_usertable_wash_2017-08-23 14:30:28.288912+10.csv'

i have googled many times come solutions runs command line

if want use once or not regularly, use postgres do. but, if use script regularly, should write pl.

either way, should this:

do $$ declare variable text; begin variable := to_char(now(), 'yyyy-mm-dd_hh24:mi:ss'); execute format ('copy (select * public.view_report_teamlist_usertable_wash)     ''d:/sf/reports/view_report_teamlist_usertable_wash_%s.csv''    delimiter '',''    csv header encoding ''utf8'' force quote *',--  // %s replaced string variable         variable -- file name     ); end $$; -- // note '' escaping ' 

edit: runs inline other queries.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -