加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-15 21:18:38 所属栏目:安全 来源:网络整理
导读:我想编写一个 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/ // t
我想编写一个 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: ****

解决方法

为什么不通过向.profile添加以下行来更新PATH并永久导出LD_LIBRARY_PATH:

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.

在此之后,您可以使用上面的脚本连接到您的数据库而无需密码提示.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读