psql: error: could not connect to server: 拒绝连接解决

在部署启动postgresql数据库时遇到这样的问题
通过以下方式得以解决

[pg13@machine ~]$ psql -d postgres
psql: error: could not connect to server: 拒绝连接
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

1.检查并关闭防火墙状态

[root@machine postgresql-13.5]# systemctl stop firewalld.service
[root@machine postgresql-13.5]# systemctl disable firewalld.service

2.检查服务是否启动,我这边是发现并没有启动

[pg13@machine ~]$ pg_ctl status
pg_ctl: no server running

启动

[pg13@machine ~]$ pg_ctl start
***中间省略***
 done
server started

3.加载环境变量


[pg13@machine ~]$ source /home/pg13/.bash_profile
[pg13@machine ~]$ source /home/pg13/.bashrc

4.重新连接,成功

[pg13@machine ~]$ psql -d postgres
psql (13.5)
Type "help" for help.