postgresql免密登录 plsql免密登录_PostgreSQL

这篇文章记录一下使用PostgreSQL的psql客户端免密码登录的几种方法。


目录

  • 环境说明
  • 现象
  • 方法1:使用环境变量PGPASSWORD
  • 方法2: 客户端个人目录下的.pgpass文件
  • 方法3: 修改服务器端配置文件
  • 总结



环境说明

环境设定详细可参看下文:



现象

可以看到缺省情况下,是需要通过提示的方式让用户输入密码的。

liumiaocn:postgres liumiao$ psql -h localhost -p 5432 postgres postgres
Password for user postgres: 
psql (12.4)
Type "help" for help.

postgres=#

方法1:使用环境变量PGPASSWORD

可以通过设定环境变量PGPASSWORD,其中设定为密码,然后export出来之后,psql就会使用此环境变量的值了。

liumiaocn:postgres liumiao$ export PGPASSWORD=liumiaocn;psql -h localhost -p 5432 postgres postgres;
psql (12.4)
Type "help" for help.

postgres=#

方法2: 客户端个人目录下的.pgpass文件

通过提供客户端个人目录下的.pgpass文件,在此文件中提供相关信息,从而使得psql在执行时能够找到密码不再提示输入,格式信息如下所示:

格式信息:主机名或者IP:端口:数据库名:用户名:密码

另外还需要注意权限必须是600,否则也不起作用,因为此密码明文保存,在文件访问时600权限能够保证Owner之外的用户无法查看内容,在操作系统层面上对密码的安全做了一定的控制,算是聊胜于无。如果不满足的话, 是不会起作用的,比如644的权限的情况下:

liumiaocn:~ liumiao$ ls -l ${HOME}/.pgpass
-rw-r--r--  1 liumiao  staff  43 Aug 31 07:20 /Users/liumiao/.pgpass
liumiaocn:~ liumiao$ cat ${HOME}/.pgpass
localhost:5432:postgres:postgres:liumiaocn
liumiaocn:~ liumiao$ psql -h localhost -p 5432 postgres postgres
WARNING: password file "/Users/liumiao/.pgpass" has group or world access; permissions should be u=rw (0600) or less
Password for user postgres:

只修改一下权限为600,即可成功

liumiaocn:~ liumiao$ chmod 600 ${HOME}/.pgpass
liumiaocn:~ liumiao$ ls -l ${HOME}/.pgpass
-rw-------  1 liumiao  staff  43 Aug 31 07:20 /Users/liumiao/.pgpass
liumiaocn:~ liumiao$ psql -h localhost -p 5432 postgres postgres
psql (12.4)
Type "help" for help.

postgres=#

方法3: 修改服务器端配置文件

事前验证,此种方式无需修改客户端,只需在服务器端进行设定并进行reload操作即可,可先行启动一个终端,并确认当前状态下是需要输入密码的:

liumiaocn:~ liumiao$ psql -h localhost -p 5432 postgres postgres
Password for user postgres:

然后通过修改服务器端配置文件也可达到免密效果,修改对象文件名如下:

修改文件:${PGDATA}/pg_hba.conf

添加如下修改内容即可(可以做很多限定,这里只限定用户,加之环境是从宿主机访问的缘故所以为127.0.0.1):

host all postgres 0.0.0.0/0 trust

liumiaocn:postgres liumiao$ docker exec -it postgres sh
/ # ls ${PGDATA}/pg_hba.conf
/var/lib/postgresql/data/pg_hba.conf
/ # 
/ # cp -p /var/lib/postgresql/data/pg_hba.conf /var/lib/postgresql/data/pg_hba.conf.org
/ # vi /var/lib/postgresql/data/pg_hba.conf
/ # diff /var/lib/postgresql/data/pg_hba.conf /var/lib/postgresql/data/pg_hba.conf.org
--- /var/lib/postgresql/data/pg_hba.conf
+++ /var/lib/postgresql/data/pg_hba.conf.org
@@ -88,7 +88,6 @@
 local   all             all                                     trust
 # IPv4 local connections:
 host    all             all             127.0.0.1/32            trust
-host    all             postgres        0.0.0.0/0               trust
 # IPv6 local connections:
 host    all             all             ::1/128                 trust
 # Allow replication connections from localhost, by a user with the
/ # chown postgres:root /var/lib/postgresql/data/pg_hba.conf

使用postgres用户执行pg_ctl reload -D /var/lib/postgresql/data/pg_hba.conf也可以,本文为了简单,直接重启容器

liumiaocn:postgres liumiao$ docker restart postgres
postgres
liumiaocn:postgres liumiao$

然后外部再次验证连接, 发现客户端在什么都没有做的情况下就已经可以登录了

liumiaocn:postgres liumiao$ psql -h localhost -p 5432 postgres postgres
psql (12.4)
Type "help" for help.

postgres=#

总结

这篇文章介绍了三种免密的方式,其中以第三种影响最大,而且给出的例子中也未做到最小权限,在实际使用中自然需要更加谨慎,以防止操作权限过大导致的实际问题。