0

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

I mean how to set the password word in the CLI ( by expect or other way ) so I will not enter the password

goal - I need to run this psql from bash script

 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)

I am also try this but without success - why ?

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

second

how to capture the first word after "blueprint_name"

meanwhile I use this but not satisfied about this approach

psql -U ambari ambari -c "select * from blueprint" | grep -v row | tail -2 | awk '{print $1}'
Password for user ambari:
HDP
tk421
  • 5,775
  • 6
  • 23
  • 34
enodmilvado
  • 443
  • 1
  • 9
  • 20

2 Answers2

2

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

Yes it's possible:

  1. set the PGPASSWORD environment variable. Here is the manual (http://www.postgresql.org/docs/current/static/libpq-envars.html)

  2. use a .pgpass file to store the password. Here is the manual (http://www.postgresql.org/docs/current/static/libpq-pgpass.html)

  3. use "trust authentication" for that specific user (http://www.postgresql.org/docs/current/static/auth-methods.html#AUTH-TRUST)

  4. use a connection URI that contains everything (http://www.postgresql.org/docs/current/static/libpq-connect.html#AEN42532)

Doggo
  • 2,133
  • 3
  • 16
  • 28
  • https://stackoverflow.com/questions/6405127/how-do-i-specify-a-password-to-psql-non-interactively – Doggo Jan 10 '18 at 13:44
1

Need to use this "-d ambari" to tell the database name as "ambari"

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

example:

 # su - postgres -c "psql -d ambari -tc 'select * from ambari.blueprint' "
 HDP  | NONE          |                               |        6
enodmilvado
  • 443
  • 1
  • 9
  • 20