How not to enter the password when connecting to the database with psql in the shell

How not to enter a password when psql connects to the database.
Application Scenario
Under the Linux platform, use psql to connect to the database in the form of batch and script

http://www.odin.hyork.net/write/write0394.html

 

Two methods
1. The use of the $HOME/.pgpass file, which can record the passwords of multiple databases in the form of plural lines
$ vi ~/.pgpass
$ chmod 600 ~/.pgpass
hostname:port:DBname:role name: password

 

2. The setting of the PGPASSWD environment variable
can be set as follows in ~/.bash_profile, .bashrc
or directly enter the following command in the script

export PGPASSWD=password

example:

#!/bin/bash

export PGPASSWORD=*****

codeReturn=$(psql -d sbank001 -p 51001 -h sabcdb10-db -U sbank001user -c "select 1 as dispname")
echo "codeReturn:"$codeReturn

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326277143&siteId=291194637