linux + 如何在不输入密码的情况下获取psql结果

linux + how to get psql results without enter password

是否可以在不输入密码的情况下运行 psql?

我的意思是如何在 CLI 中设置密码字(通过 expect 或其他方式)所以我不会输入密码

目标 - 我需要 运行 来自 bash 脚本的这个 psql

 psql -U ambari ambari -c "select * from blueprint"  --> HDP
 Password for user ambari:
 blueprint_name | security_type | security_descriptor_reference | stack_id
----------------+---------------+-------------------------------+----------
   HDP          | NONE          |                               |        2
 (1 row)

我也在尝试这个但没有成功 - 为什么?

su - postgres -c "     psql -tc \"SELECT * FROM BLUEPRINT\" "
ERROR:  relation "blueprint" does not exist
LINE 1: SELECT * FROM BLUEPRINT
                  ^

如何捕获"blueprint_name"

后的第一个词

同时我使用了这个但对这种方法不满意

psql -U ambari ambari -c "select * from blueprint" | grep -v row | tail -2 | awk '{print }'
Password for user ambari:
HDP

Is it possible to run the psql without enter the password ?

有可能:

  1. 设置 PGPASSWORD 环境变量。这是手册(http://www.postgresql.org/docs/current/static/libpq-envars.html)

  2. 使用 .pgpass 文件存储密码。这是手册(http://www.postgresql.org/docs/current/static/libpq-pgpass.html)

  3. 对特定用户 (http://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-TRUST) 使用 "trust authentication"

  4. 使用包含所有内容的连接 URI (http://www.postgresql.org/docs/current/static/libpq-connect.html#AEN42532)

需要使用这个“-d ambari”来告诉数据库名称为"ambari"

 # su - postgres -c "psql -d ambari -tc 'select * from ambari.blueprint' 

示例:

 # su - postgres -c "psql -d ambari -tc 'select * from ambari.blueprint' "
 HDP  | NONE          |                               |        6