Several methods for clients to connect to PostgreSQL without entering a password (transfer)

 In normal work, sometimes it is necessary to connect to the PostgreSQL database in different places to do some maintenance, such as remote backup, etc.
If the backup script is written on a remote machine, a password input prompt will pop up during backup, so the script cannot be
executed . Here are some different methods. How to pop up a password input prompt.


--Test environment
target library IP: 192.168.1.25/1921; database skytf
client IP: 192.168.1.26

--Connect to database skytf at 192.168.1.26, pop up password prompt
postgres@db6 -> psql -h 192.168.1.25 -p 1921 skytf skytf
Password for user skytf:

 

Method 1: Set the environment variable PGPASSWORD   

    PGPASSWORD is a PostgreSQL system environment variable. After setting this on the client
, this password will be used preferentially when the client connects to the remote database.



--test postgres@db6 - > export PGPASSWORD=skytf                

postgres@db6-> psql -h 192.168.1.25 -p 1921 skytf skytf
psql (9.1beta3, server 9.0.1)                          
WARNING: psql version 9.1, server version 9.0.         
         Some psql features might not work.            
Type "help" for help.                                  
                                                       
skytf=> \q             

     Remarks: Set the environment variable PGPASSWORD , the password input prompt will no longer pop up when connecting to the database. However, from a security point
                of view, this method is not recommended.


Method 2: Set the .pgpass password file
       
       By creating a hidden file .pgpass in the /home/postgres directory of the client, to avoid a
 password input prompt when connecting to the database.

--创建密码文件 .pgpass ( on 客户端 )                               
vi /home/postgres/.pgpass       
                                     
--格式                                                               
hostname:port:database:username:password                             
                                                                     
--范例                                                               
192.168.1.25:1921:skytf:skytf:skytf                                   
                                                                     
--权限                                                               
Chmod 600 .pgpass                  

--连接测试
postgres@db6-> psql -h 192.168.1.25 -p 1921 skytf skytf
psql (9.1beta3, server 9.0.1)
WARNING: psql version 9.1, server version 9.0.
         Some psql features might not work.
Type "help" for help.

skytf=>

      备注:在/home/postgres 目录创建了密码文件 .pgpass 文件后,并正确配置连接信息,
                那么客户端连接数据时会优先使用 .pgass文件, 并使用匹配记录的密码,从而
                不跳出密码输入提示,这种方法比方法一更安全,所以推荐使用创建 .pgpass 文
               件方式。                                

                               
方法三:修改服务端 pg_hba.conf

     修改认证文件 $PGDATA/pg_hba.conf, 添加以下行, 并 reload使配置立即生效。
     
host    skytf          skytf            172.16.3.174/32           trust

[postgres@192_168_1_26 pg_root]$ pg_ctl reload -D $PGDATA
server signaled

--服务端 pg_hba.conf 的配置
# IPv4 local connections:
host    all             all             127.0.0.1/32             trust
host    skytf          skytf            172.16.3.174/32           trust
host    all             all             0.0.0.0/0                md5

--客户端再次连接测试
postgres@db6-> psql -h 192.168.1.25 -p 1921 skytf skytf
psql (9.1beta3, server 9.0.1)
WARNING: psql version 9.1, server version 9.0.
         Some psql features might not work.
Type "help" for help.

skytf=> \q

      备注:修改服务端 pg_hba.conf 并 reload 后,不再弹出密码输入提示。

Guess you like

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