I'm trying to do a script that runs queries in PostgreSQL. I'm struggling with something that seems pretty simple (but does not work...).
- I have a set of queries stored as an array of strings, lets' say
query1="SELECT COUNT(id) FROM nodes WHERE label='KeywordList';"
query2="SELECT COUNT(id) FROM nodes WHERE label='AuthorList';"
queries=($query1 $query2)
- Then, I want to run this query in PSQL through a loop:
for query in "${queries[@]}"
do
psql $db_name -c "$query"
done
This gives me the following error:
ERROR: syntax error at or near "COUNT"
LINE 1: COUNT(id)
^
ERROR: syntax error at or near "FROM"
LINE 1: FROM
^
ERROR: syntax error at or near "nodes"
LINE 1: nodes
^
ERROR: syntax error at or near "WHERE"
LINE 1: WHERE
^
ERROR: syntax error at or near "label"
LINE 1: label='KeywordList';
- But this works when I give to PSQL the string itself:
psql $db_name -c "$query1"
(this returns me10
).
I think that this is a quoting problem, or maybe Bash tries to understand what is in the string... I can not resolve this on my own after many tries with double, single and back quotes.
Help will be really appreciated, Nelly.