I am facing problem with executing postgresql queries on remote machine from local.
Shell script which I am running on local machine:
[postgres@local:~]$ cat main.sh
#!/bin/bash
REMOTE="[email protected]"
PKEY="/home/postgres/pr-key.ppk"
SCRIPT_LOCATION=/home/postgres/scripts
CRONLOG_LOCATION=/home/postgres/project/cronlogs
PGBIN=/usr/pgsql-9.6/bin
PGUSER=postgres
PGDATA=/opt/PostgreSQL/9.6/data
PGDATABASE=postgres
PGPORT=5432
PGHOST=192.168.1.200
BLOG=/home/postgres/blogfile
query=`ssh -i $PKEY $REMOTE $PGBIN/psql -U $PGUSER -h $PGHOST -p $PGPORT -d $PGDATABASE -Atc "select datname from pg_database where datname not in ('template0','template1');"`
echo $query
Execution:
[postgres@local:~]$ ./main.sh
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `/usr/pgsql-9.6/bin/psql -U postgres -h 192.168.1.200 -p 5432 -d postgres -Atc select datname from pg_database where datname not in ('template0','template1');'
bash: -c:... -Atc select datname...queryand then expanding that variable unquoted? Consider running your code through shellcheck.net and fixing what it finds.-xflag) and post last lines? why viassh(postgres available only on local machine)?-Atc '"select ...('template0','template1');"'