简体   繁体   中英

Export PSQL table to CSV file using cmd line args

I am attempting to export my PSQL table to a CSV file. I have read many tips online, such as Save PL/pgSQL output from PostgreSQL to a CSV file

From these posts I have been able to figure out

\copy (Select * From foo) To '/tmp/test.csv' With CSV

However I do not want to specify the path, I would like the user to be able to enter it via the export shell script. Is it possible to pass args to a .sql script from a .sh script?

I was able to figure out the solution.

In my bash script I used something similar to the following

psql $1 -c "\copy (SELECT * FROM users) TO '"$2"'" DELIMINTER ',' CSV HEADER"

This code copies the user table from the database specified by the first argument and exports it to a csv file located at the second arguement

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM