Postgresql 错误语法(从shell脚本执行psql查询)

Postgresql 错误语法(从shell脚本执行psql查询),postgresql,shell,psql,Postgresql,Shell,Psql,我得到这个查询,希望在我的第二台服务器上远程执行,并且 #!/bin/bash QUERY=`psql -h my2ndserverip -d testdb -U testuser 'select count(*) as result from testdb.user where last_logged_date > (clock_timestamp() -interval '90 minutes) echo "users = $QUERY" > tmp.txt

我得到这个查询,希望在我的第二台服务器上远程执行,并且

#!/bin/bash
QUERY=`psql -h my2ndserverip  -d testdb -U testuser  'select count(*) as result   
from testdb.user  where last_logged_date > (clock_timestamp()  -interval '90    minutes)
 echo "users = $QUERY" > tmp.txt

有修复语法的提示吗?

有不止一个问题

您可以使用$$

postgres=# select interval $$90 min$$;
 interval 
──────────
 01:30:00
(1 row)
使用here文档(heredocuments保留引号并允许shell变量替换,如单引号内使用的参数
90
所示):

#/bin/bash
限制=${1:-90}
psql-h my2ndserverip-d testdb-U testuser-t-A clock_timestamp()-间隔${limit}分钟
;
伊奥菲奥夫
exitcode=$?
结果=`cat tmp.txt`
echo“Limit=${Limit}Exitcode=${Exitcode}Result=${Result}”
#Eof
我想您希望psql省略列标题等,因此我将
-t-A
标志添加到psql命令行


顺便说一句,我从testdb.user更改为
,从user
更改为
,我认为您没有名为“testdb”的模式。

不确定这是否是唯一的quotesSure,但这将是一个很好的开始。太好了,您能告诉我哪里漏了引号吗?但我正在用shell脚本运行查询,并将echo输出到一个文件是的,我知道,你的问题是嵌套引号使用不当。因此,请使用$$而不是嵌套引号。”选择。。90年代的间隔。。。缺少“缺少第二个”…-->选择间隔$$90分钟$$'
#!/bin/bash

limit=${1:-90}

psql -h my2ndserverip  -d testdb -U testuser -t -A <<EOFEOF > tmp.txt
  SELECT count(*) AS result   
  FROM "user"  
  WHERE last_logged_date > clock_timestamp()-interval '${limit} minutes'
  ;
EOFEOF

exitcode=$?
result=`cat tmp.txt`

echo "Limit=${limit} Exitcode=${exitcode} Result=${result}"
#Eof