我在从本地远程计算机上执行 postgresql 查询时遇到问题。
我在本地计算机上运行的 Shell 脚本:
[postgres@local:~]$ cat main.sh
#!/bin/bash
REMOTE="postgres@192.168.1.200"
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
执行:
[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');'
最佳答案
ssh 允许您运行存储在 ssh 客户端上的脚本文件,该脚本文件在远程计算机上运行,就好像它实际上在远程计算机上一样,这使得脚本编写更加容易。如果您可以让 progree.sql.sh 文件在本地上下文中工作,那么
ssh user@hostname 'bash -s' < sql.script_on_this_machine.sh
我使用 key ,所以我不必担心这里的密码。
关于linux - 不使用 ssh 在远程计算机上执行 postgresql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48127483/