I have the following snippet of code in a bash script:
psql -U ppd ppd -h $remote_server -t -c "copy (SELECT CASE WHEN DATE_TRUNC('MONTH', ABSTIME(start_ts)) = DATE_TRUNC('MONTH', CURRENT_DATE) THEN 'current' ELSE DATE_TRUNC('MONTH', ABSTIME(start_ts))::DATE::TEXT END,
id,
status,
class,
dir,
volid,
f_user,
t_user,
ABSTIME(start_ts),
ABSTIME(end_ts),
elapsed,
callid, f_uri, t_uri, lcr_tag
FROM ppd_widgets) to '/tmp/"$server".db' With CSV DELIMITER ',';"
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "root@$server:/tmp/$server.db" "$dir/"
I'd like to know if there's a way to do it in one shot - aka instead of creating a csv file on the remote server and then copy it over, can I select the records and display in csv format?
Then I could do something like this:
psql -U ppd ppd -h $remote_server -t -c "select that returns in csv style" >> save_to_local.csv
Any tips / suggestions on how I can do this?