1

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?

1 Answer 1

1

Use psql's \copy function (not PostgreSQL's copy function)

This lets you load or save files locally

Example:

psql -c "\copy (select ...) to 'local-file.csv'"
Sign up to request clarification or add additional context in comments.

3 Comments

so that will run the query against the remote server defined by "-h" but save the file locally on the machine where the psql command is triggered? Will give it a go right now
@dot yes if you set the remote host name it will run that query against that server, but save the file locally. \copy also has the advantage of not requiring superuser privileges
yes, it worked perfectly! I have another kinda similar question ... here: stackoverflow.com/questions/49307088/… - if you don't mind helping out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.