Reputation: 550
hi guys I am trying to get data from psql using shellscript here are my command which is working fine
PGPASSWORD=nifi psql -U nifi -d MPT_BI -h 10.10.101.10
\copy (select * from dwctr.tcmpt_usage_cellid_daily where stat_date='20200502' limit 10) TO '/home/bi_it/csv_export/gp_export/dwctr.tcmpt_usage_cellid_dailny_20200502.csv' CSV DELIMITER '^'
\q
but when I am tryiyng to automate using shell i am getting error here is my shell script
#!/bin/bash
dir='/home/bi_it/csv_export/gp_export'
cd $dir
PGPASSWORD=nifi psql -U nifi -d MPT_BI -h 10.10.101.10
\copy (select * from dwctr.tcmpt_usage_cellid_daily where stat_date='20200502' limit 10) TO '/home/bi_it/csv_export/gp_export/dwctr.tcmpt_usage_cellid_dailny_20200502.csv' CSV DELIMITER '^'
\q
error
./newgp.sh: line 6: syntax error near unexpected token `select'
./newgp.sh: line 6: `\copy (select * from dwctr.tcmpt_usage_cellid_daily where stat_date='20200502' limit 10) TO '/home/bi_it/csv_export/gp_export/dwctr.tcmpt_usage_cellid_dailny_20200502.csv' CSV DELIMITER '^''
can any one help me with this
Upvotes: 0
Views: 103
Reputation: 246268
Use a "here document":
#!/bin/bash
dir='/home/bi_it/csv_export/gp_export'
cd "$dir"
PGPASSWORD=nifi psql -U nifi -d MPT_BI -h 10.10.101.10 <<EOF
\copy (select * from dwctr.tcmpt_usage_cellid_daily where stat_date='20200502' limit 10) TO '/home/bi_it/csv_export/gp_export/dwctr.tcmpt_usage_cellid_dailny_20200502.csv' CSV DELIMITER '^'
\q
EOF
Everything up to the final EOF
will become the standard input for psql
.
Upvotes: 1