简体   繁体   English

ShellScript的PSQL COPY

[英]PSQL COPY from ShellScript

I am writing a shell script that fetches data (.csv file) form AWS S3, downloads it locally onto an EC2 Linux AMI Instance, and then copies the data to an RDS PostGresql database. 我正在编写一个Shell脚本,该脚本从AWS S3提取数据(.csv文件),将其本地下载到EC2 Linux AMI实例,然后将数据复制到RDS PostGresql数据库。

My Shell code is the following: 我的Shell代码如下:

FILE="$(ls DB)"
PARAMETERFORDB= "'\\COPY table(x,y) FROM ''$FILE'' CSV HEADER'"

$(psql --host=XXXXX --port=XXXXX --username=XXXXX --password --dbname=XXXXX -c ${PARAMETERFORDB})

So when the data from S3 is downloaded, I store the files' name inside the FILE variable (it is the only file in the folder, the folder will be deleted after the Database query). 因此,从S3下载数据时,我将文件名存储在FILE变量中(这是文件夹中的唯一文件,在数据库查询后该文件夹将被删除)。

I get following error message: 我收到以下错误消息:

./shellTest.sh: line 21: '\COPY table(x,y) FROM ''14.9.2016.csv'' CSV HEADER': command not found
psql: option requires an argument -- 'c'
Try "psql --help" for more information.

What am I doing wrong? 我究竟做错了什么?

In the line 在行中

PARAMETERFORDB= "'\\COPY table(x,y) FROM ''$FILE'' CSV HEADER'"

remove the space after the = and remove one level of single quotes: 删除=后面的空格并删除单引号的一级:

PARAMETERFORDB="\\COPY table(x,y) FROM '$FILE' CSV HEADER"

In the line where psql is invoked, enclose ${PARAMETERFORDB} in double quotes since it contains spaces. 在调用psql的行中,将${PARAMETERFORDB}用双引号引起来,因为它包含空格。

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

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