1

If I remote into a postgres database on heroku with heroku pg:psql -a appname I can wait a few seconds for it to connect, then run \copy (SELECT * FROM users) TO users.csv CSV DELIMITER ',' HEADER to extract a table from the remote database to my desktop.

However, if I put these two lines of code into a .bat file and run it, it opens cmd.exe and connects to the postgres database (as expected), but then nothing happens (no more code is executed)

How do you construct the .bat file so that it runs the first line (to connect to the remote database), then executes the subsequent lines inside the remotely connected database?

1 Answer 1

1

You would have to pass the command to psql with the -c switch.

psql -c "\copy (SELECT * FROM users) TO users.csv CSV DELIMITER ',' HEADER"

If you need to execute more commands it will be easier to put them in a seperate file and pass that to psql with the -f switch.

Sign up to request clarification or add additional context in comments.

Comments

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.