简体   繁体   English

使用cmd line args将PSQL表导出为CSV文件

[英]Export PSQL table to CSV file using cmd line args

I am attempting to export my PSQL table to a CSV file. 我正在尝试将我的PSQL表导出为CSV文件。 I have read many tips online, such as Save PL/pgSQL output from PostgreSQL to a CSV file 我在网上看过很多提示,例如将PostgreSQL的PL / pgSQL输出保存到CSV文件

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. 但是我不想指定路径,我希望用户能够通过export shell脚本输入它。 Is it possible to pass args to a .sql script from a .sh script? 是否可以从.sh脚本将args传递给.sql脚本?

I was able to figure out the solution. 我能够找到解决方案。

In my bash script I used something similar to the following 在我的bash脚本中,我使用了类似于以下内容的东西

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 此代码从第一个参数指定的数据库中复制用户表,并将其导出到位于第二个参数的csv文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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