我想编写一个shell脚本来运行这些命令。我通常使用如下命令从终端连接
//first go to the directory
cd /opt/novell/sentinel/3rdparty/postgresql/bin/
// then type following
export LD_LIBRARY_PATH=/opt/novell/sentinel/3rdparty/postgresql/lib/
// then fire following command
./psql --host 127.0.0.1 --port 5432 --dbname=SIEM --username=dbauser
Password for user dbauser: ****
最佳答案
你为什么不更新你的 PATH 并永久导出 LD_LIBRARY_PATH,将这些行添加到 .profile
中:
PATH=/opt/novell/sentinel/3rdparty/postgresql/bin/:$PATH
export LD_LIBRARY_PATH=/opt/novell/sentinel/3rdparty/postgresql/lib/
然后使用脚本连接DB如下简单
#!/bin/sh
psql --host=127.0.0.1 --port=5432 --dbname=SIEM --username=dbauser
运行脚本后,系统会询问您密码。
如果你不想每次都输入密码,你可以使用密码文件.pgpass(详见documentation),只需在你的
~/.pgpass
中添加以下行:127.0.0.1:5432:SIEM:dbauser:your_password
安全起见,禁止任何对世界或组的访问:
chmod 0600 ~/.pgpass.
在此之后,您可以在没有密码提示的情况下使用上面的脚本连接到您的数据库。
关于shell - 如何使用shell脚本连接到postgresql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264131/