淘先锋技术网

首页 1 2 3 4 5 6 7

目录

1、遇到问题

 2、问题分析

3、问题解决

4、注意


1、遇到问题

psql -U postgres -d postgres -h 192.168.88.198 -p 5432

psql: error: could not connect to server: 拒绝连接
    Is the server running on host "192.168.88.198" and accepting
    TCP/IP connections on port 5432?

 2、问题分析

  • 首先查看端口5432占用情况。
netstat -anp | grep 5432

 pgsql默认监听的是localhost或127.0.0.1,目前只能本机访问。

如果远程访问就连接不上了,具体的配置文件是/etc/postgresql/12/main目录下的postgresql.conf,可以通过修改这个配置文件来调整各个参数。

比如:listen_addresses可以修改绑定的地址,默认是localhost,port可以修改监听的端口号,默认是5432,max_connections可以修改最大客户端连接数量,默认是100等等。

3、问题解决

先把postgresql服务停止。

sudo systemctl stop postgresql
  • 进入目录/etc/postgresql/12/main
cd /etc/postgresql/12/main
ls

  •  修改文件postgresql.conf
listen_addresses = '*'		        # what IP address(es) to listen on;

  •  修改文件pg_hba.conf
host    all             all             192.168.88.198/32       md5
host    all             all             0.0.0.0/0               md5

第一种格式:host  all  all 192.168.88.198/32  md5

可以设置指定ip连接到数据库服务上。

第二种格式:host  all  all 0.0.0.0/0  md5

任意外部ip都可访问到数据库服务。

上述方式选择其一即可。

 

  •  重启postgresql服务
sudo systemctl start postgresql

  •  远程连接
psql -U postgres -d postgres -h 192.168.88.198 -p 5432

 成功解决问题。

4、注意

host    all             all             192.168.88.198/32       md5
host    all             all             0.0.0.0/0               md5

语法

local <dbname> <user> <auth-method> [auth-options]

host <dbname> <user> <ip/masklen> <auth-method> [auth-options]