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

标签 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 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/

相关文章:

bash - 退出管道中的 BASH 无限循环

linux - 遍历 bash 中的参数

linux - 帮助我从 ABS 指南中理解 bash 中的这个简单的 io 重定向

database - 这个 postgres 查询是怎么工作的?

database - 如何将 *.dump 文件中的表提取到 CSV 中

bash:PROMPT_COMMAND 不执行多个命令,单个命令有效

java - 我可以从网页执行 shell 脚本吗?

psql - Prisma 与 psql db - 不兼容的类型 : text and uuid

postgresql - 如何从 psql 提示符将双引号更改为单引号

PostgreSQL 无法创建用户