linux - 不使用 ssh 在远程计算机上执行 postgresql 查询

标签 linux bash postgresql ssh sh

我在从本地远程计算机上执行 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/

相关文章:

python fabric 如何在远程机器上启动进程

bash HISTSIZE 与 HISTFILESIZE?

postgresql - 返回游标结果

linux - 将一个文件附加到另一个文件中的一个衬里,但前提是它尚未被添加

linux - ccmake。与 ccmake [路径]

linux - PATH 和 COMPILER_PATH 之间的区别

python - 如何在 Python 中运行 bash 脚本,但表现得好像它是从另一个目录运行的?

linux - 重命名目录中的所有文件,文件名部分加1

postgresql - 如何使用postgresql测试数据库表中的相关行已被锁定

SQL在Groovy中执行不等到它完成才返回