数据库教程:PostgreSQL 允许远程访问设置的操作

postgres远程连接方式配置配置pg_hba.conf文件 目录c:program filespostgresql9.5data(qxy)主机[postgres@qxy data]$ pw


postgres远程连接方式配置

配置pg_hba.conf文件 目录c:program filespostgresql9.5data

  (qxy)主机  [postgres@qxy data]$ pwd  /spark/pgsql/data  [postgres@qxy data]$ cat pg_hba.conf  # type database user address method  # “local” is for unix domain socket connections only  local all all trust  # ipv4 local connections:  host all all 127.0.0.1/32 trust  # ipv6 local connections:  host all all ::1/128 trust

在# ipv4 local connections:

下面添加一行,内容为 “host all all 192.168.40.1/24 md5”,代表192.168.40网段的ip地址的所有用户都可以连接,/24代表网段,如果是/32 需要写完整的ip地址

添加之后的内容如下:

  # “local” is for unix domain socket connections only  local all all trust  # ipv4 local connections:  host all all 127.0.0.1/32 trust  host all all 192.168.40.1/24 md5

2. 修改postgres监听的ip地址,默认是localhost ———- 有时候默认就是ok的,可以看看用不用修改

  (qxy)主机  postgres@qxy data]$ cat postgresql.conf  # - connection settings -  #listen_addresses = ‘localhost'  # what ip address(es) to listen on;  listen_addresses = ‘*'  # what ip address(es) to listen on; <=======新增这行,把localhost改成*,监听所有的ip  # comma-separated list of addresses;  # defaults to ‘localhost'; use ‘*' for all  # (change requires restart)  

3.重新启动postgres 服务

在service服务list中重启: postgresql-x64-9.5

4. 大功告成,远程测试下连接postgresdb吧

补充:postgresql允许postgres用户在一个特定的ip进行远程登录,并具有所有库任何操作权限

1.pgsql允许远程访问:

安装postgresql数据库之后,默认是只接受本地访问连接。如果想在其他主机上访问postgresql数据库服务器,就需要进行相应的配置。

a.如果是windows安装的postgresql,配置远 程连接postgresql数据库的步骤很简单,只需要修改安装目录data文件夹下的pg_hba.conf和postgresql.conf。

PostgreSQL 允许远程访问设置的操作

b.如果是linux上安装的postgresql,同样是修改是这两个文件:

文件位置:

cd /etc/postgresql/9.3/main/

PostgreSQL 允许远程访问设置的操作

2.修改pg_hba.conf文件,配置用户的访问权限(#开头的行是注释内容):

  # database administrative login by unix domain socket  local  all       postgres                peer  # type database    user      address         method  # "local" is for unix domain socket connections only  local  all       all                   trust  # ipv4 local connections:  host  all       all       127.0.0.1/32      trust  host  all       all       222.73.203.68/24        trust  # ipv6 local connections:  host  all       all       ::1/128         trust  # allow replication connections from localhost, by a user with the  # replication privilege.  #local  replication   postgres                peer  #host  replication   postgres    127.0.0.1/32      md5  #host  replication   postgres    ::1/128         md5  

其中,第10条是新添加的内容,表示允许网段 222.73.203.68上的这个特定主机使用所有合法的数据库用户名访问数据库。

如果允许所有主机访问,不需要特别严格的权限控制时,可以直接将第10行,ipv4中换成:

host all all 0.0.0.0/0 trust 即可

3.修改postgresql.conf文件,将数据库服务器的监听模式修改为监听所有主机发出的连接请求:

定位到#listen_addresses=’localhost’。postgresql安装完成后,默认是只接受来在本机localhost的连接请 求。

将行开头都#去掉,将行内容修改为listen_addresses=’*’来允许数据库服务器监听来自任何主机的连接请求

4.查看postgresql默认端口号:(一般默认端口号是:5432)

PostgreSQL 允许远程访问设置的操作

5.查看防火强状态:

PostgreSQL 允许远程访问设置的操作

如果防火墙active是开启状态,允许5432端口入站:

PostgreSQL 允许远程访问设置的操作

查看防火墙状态,查看5432端口是否增加成功:

PostgreSQL 允许远程访问设置的操作

6.回到 222.73.203.68这台机器上的客户端就可以对所要远程机器上的数据库进行远程访问了

PostgreSQL 允许远程访问设置的操作

以上为个人经验,希望能给大家一个参考,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。如有错误或未考虑完全的地方,望不吝赐教。

需要了解更多数据库技术:PostgreSQL 允许远程访问设置的操作,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

www.ctvol.com true Article 数据库教程:PostgreSQL 允许远程访问设置的操作

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/632082.html

(0)
上一篇 2021年5月31日 上午4:52
下一篇 2021年5月31日 上午4:55

精彩推荐