在之前的博客,我们看了PG的安装,如下:
PostgreSQL 学习笔记(3) — PG 单实例安装手册
https://www.cndba.cn/dave/article/116374
但是用DBeaver连接的时候,会报如下错误:
Connection to 192.168.56.101:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Connection refused: connect
Connection refused: connect
这是因为pg安装之后默认的监听是启动在127.0.0.1上,需要为pg设置合理的监听端口和外部访问策略才可以通过远程的方式进行访问。
这里需要修改2个文文件。
1. 修改postgresql.conf,在改文件中设置数据库的监听地址和端口。
-bash-4.2$ ps -ef|grep pg
postgres 1082 1 0 10:40 ? 00:00:00 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
postgres 2253 2185 0 10:48 pts/0 00:00:00 grep --color=auto pg
-bash-4.2$
postgresql.conf 设置 如下:
cat >> /var/lib/pgsql/14/data/postgresql.conf <<"EOF"
listen_addresses = '*' #监听启动在本机的所有IP地址上
port=5432 #监听的服务端口
unix_socket_directories='/var/lib/pgsql/14/data/'
logging_collector = on #后面几个参数都是监听的日志情况
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF
2. 修改pg_hba.conf文件,设置数据库的远程访问策略
pg_hba.conf 访问策略的设置如下:
cat << EOF > /var/lib/pgsql/14/data/pg_hba.conf
local all all trust
host all all 127.0.0.1/32 trust
host all all 0.0.0.0/0 md5
host replication all 0.0.0.0/0 md5
EOF
3. 重启PG
-bash-4.2$ ps -ef|grep pg
postgres 1082 1 0 10:40 ? 00:00:00 /usr/pgsql-14/bin/postmaster -D /var/lib/pgsql/14/data/
postgres 2330 2185 0 10:53 pts/0 00:00:00 grep --color=auto pg
-bash-4.2$ pg_ctl restart -D /var/lib/pgsql/14/data/
waiting for server to shut down.... done
server stopped
waiting for server to start....2022-12-13 10:53:38.748 CST [2352] LOG: redirecting log output to logging collector process
2022-12-13 10:53:38.748 CST [2352] HINT: Future log output will appear in directory "pg_log".
done
server started
-bash-4.2$
然后就可以正常连接了:
版权声明:本文为博主原创文章,未经博主允许不得转载。