gpt4 book ai didi

shell - 如何使用shell脚本连接到postgresql数据库

转载 作者:行者123 更新时间:2023-12-04 23:44:49 25 4
gpt4 key购买 nike

我想编写一个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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com