0

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

1 Answer 1

1

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.

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.