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

如何使用shell脚本连接到postgresql数据库,shell,psql,Shell,Psql,我想编写一个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

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

为什么不更新路径并永久导出LD_LIBRARY_路径,方法是在
.profile
中添加以下行:

PATH=/opt/novell/sentinel/3rdparty/postgresql/bin/:$PATH
export LD_LIBRARY_PATH=/opt/novell/sentinel/3rdparty/postgresql/lib/
然后使用脚本连接数据库,如下所示

#!/bin/sh
psql --host=127.0.0.1 --port=5432 --dbname=SIEM --username=dbauser
运行脚本后,系统将询问您密码

如果不希望每次都输入密码,可以使用密码文件.pgpass(有关详细信息,请参阅),只需在
~/.pgpass
中添加以下行:

127.0.0.1:5432:SIEM:dbauser:your_password
注意安全,允许任何人访问世界或团体:

chmod 0600 ~/.pgpass.

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

那么,您是否同意将密码存储在shell脚本中?不,我不想将密码存储在脚本中。我在哪里创建.pgpassfile@GauravRajput,
.pgpass
文件通常位于您的主目录中,或者在由PGPASSFILE环境变量定义的目录中。我在根目录下创建了这个.pgpass文件,然后它工作正常。非常感谢您我收到了这个错误“/usr/bin/postgres:option invalide--“U”