简体   繁体   English

无法通过 bash 脚本登录到 postgres

[英]Not able to login to postgres through bash script

I have one script in which I have given following command -我有一个脚本,其中给出了以下命令 -

su - postgres su - postgres

psql查询语句

\\c db1; \\c db1;

select * from employees;从员工中选择*;

But when I am running the script I am not able to go inside psql, its stooping after first command that is "su - postgres"但是当我运行脚本时,我无法进入 psql,它在第一个命令“su - postgres”之后弯腰

[root@quadoralnx ~]# ./ora.sh [root@quadoralnx ~]# ./ora.sh

Last login: Tue Aug 25 01:23:59 EDT 2020 on pts/11上次登录: 2020 年 8 月 25 日星期二 01:23:59 EDT pts/11

-bash-4.2$ -bash-4.2$

And when I am doing exit, it is giving following error -当我退出时,出现以下错误 -

-bash-4.2$ exit -bash-4.2$ 退出

logout登出

psql: error: could not connect to server: FATAL: role "root" does not exist psql:错误:无法连接到服务器:致命:角色“root”不存在

./ora.sh: line 3: c: command not found ./ora.sh: line 3: c: command not found

./ora.sh: line 4: syntax error near unexpected token `from' ./ora.sh:第 4 行:意外标记“来自”附近的语法错误

./ora.sh: line 4: `select * from employees;' ./ora.sh: 第 4 行:`select * from Employees;'

[root@quadoralnx ~]# [root@quadoralnx ~]#

So here my need is that it should not stop in first step, and should run all the command in that script in single shot.所以在这里我的需要是它不应该在第一步中停止,并且应该一次性运行该脚本中的所有命令。 Can someone please help me with this?有人可以帮我解决这个问题吗?

Thanks in advance!!提前致谢!!

It's not going to work like this.它不会像这样工作。 Use this syntax to run sql queries from bash cli:使用以下语法从 bash cli 运行 sql 查询:

dbhost=localhost
dbport=5432
dbuser=postgres
dbname=postgres

psql -h$dbhost -p$dbport -U$dbuser -d$dbname -c "select 1;"

And to avoid password promting you can create ~/.pgpass file like this为了避免密码提示,您可以像这样创建~/.pgpass文件

dbpass=secret
echo "$dbhost:$dbport:$dbname:$dbuser:$dbpass" > ~/.pgpass
chmod 600 ~/.pgpass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM