Postgresql 配置允许远程连接

阿里云教程4个月前发布
24 0 0

postgresql 初次安装后,只允许本地连接。

我们需要修改两个配置,来允许远程客户端连接。
在Postgresql 安装路径:

/etc/postgresql/[版本号]/main

下面找到两个文件:

  • postgresql.conf
    postgresql.conf 是Postgresql 的启动配置。类似于oracle 的 pfile 文件。
  • pg_hba.conf
    pg_hba.conf 是客户端访问策略配置。用来控制客户端的连接方式、访问内容。

root@xxxserver:/etc/postgresql/13/main> ls -lh
total 56K
drwxr-xr-x 2 postgres postgres 4.0K Mar 25 04:21 conf.d
-rw-r--r-- 1 postgres postgres  315 Mar 25 04:21 environment
-rw-r--r-- 1 postgres postgres  143 Mar 25 04:21 pg_ctl.conf
-rw-r----- 1 postgres postgres 4.9K Mar 25 04:21 pg_hba.conf
-rw-r----- 1 postgres postgres 1.6K Mar 25 04:21 pg_ident.conf
-rw-r--r-- 1 postgres postgres  28K Mar 25 04:21 postgresql.conf
-rw-r--r-- 1 postgres postgres  317 Mar 25 04:21 start.conf

vim postgresql.conf

找到如下下 listen_addresses 这一行,

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATIONs
#------------------------------------------------------------------------------
# - Connection Settings -
#listen_addresses =  localhost           # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to  localhost ; use  *  for all

去掉注释,修改服务器的监听范围:

 listen_addresses =  *  #监听所有客户端

vim pg_hba.conf

在文件最后插入一行

# TYPE  DATABASE        USER            ADDRESS                 METHOD
...
# 其他配置
...
host    all             all             0.0.0.0/0               md5

文件首部的注释中解释了每行的五个参数的含义,分别是

  • TYPE : 客户端连接的类型:【local(本地) / host (tcp连接) / hostssl (ssl加密连接)】
  • DATABASE: 允许连接的数据库名称
  • USER : 允许访问的角色(用户)
  • ADDRESS :允许连接的IP地址
  • METHOD : 认证方式。可以指定拒绝(reject)该客户端连接服务器。如果允许连接,此参数指定连接认证的加密方式。设置为“password”时,密码为明文发送。

重启服务

 sudo /etc/init.d/postgresql restart

执行 lsof -i:5432 验证服务器是否启动,

root@xxxserver:/etc/postgresql/13/main> lsof -i:5432
COMMAND    PID     USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
postgres 22019 postgres    7u  IPv4 101464065      0t0  TCP *:postgresql (LISTEN)
postgres 22019 postgres    8u  IPv6 101464066      0t0  TCP *:postgresql (LISTEN)

© 版权声明

相关文章

暂无评论

none
暂无评论...