我使用以下方法安装了 PostgreSQL:
sudo apt install libpq-dev postgresql postgresql-contrib
一开始一切正常,但我还需要远程连接,
所以
我需要修改:
pg_hba.conf
和 postgresql.conf
但我会在修改之前对它们进行备份。
- 重启 -
sudo systemctl restart posgresql
有时效果很好
但在其他情况下,当我尝试 sudo -u postgres psql
时,我收到以下错误:
psql: colud not connect to the server: No such file or directory. Is
the server running locally and accepting connections on Unix domain
socket "/var/run/postgresql/.s.PGSQL.5432
这很奇怪,因为我只更改了 pg_hba.conf
中的 IP 地址以允许远程连接,有时工作没有错误,有时我收到错误。也可以远程停止工作。
我回到备份文件,重启服务器(所以远程文件没有变化),错误仍然存在。
我检查服务:sudo systemctl status postgresql
积极工作。
我不知道出了什么问题,因为我希望从备份中返回到初始文件来修复错误。请帮忙
我发现多次询问错误,但在我的情况下,服务器处于事件状态,甚至返回备份并且不工作。
我设法通过以下方法解决了这个问题。
- 检查 postgresql 日志
> tail -f /var/log/postgresql/<what-ever-postgresql-log-name>.logs
如果您的日志显示 FATAL: could not remove old lock file
如下。然后进行第 2 步。
2019-09-06 01:49:13.477 UTC [5439] LOG: database system is shut down
pg_ctl: another server might be running; trying to start server anyway
2019-09-06 01:51:17.668 UTC [1039] FATAL: could not remove old lock file "postmaster.pid": Permission denied
2019-09-06 01:51:17.668 UTC [1039] HINT: The file seems accidentally left over, but it could not be removed. Please remove the file by hand and try again.
pg_ctl: could not start server
Examine the log output.
- 删除 data_directory 路径中的 postmaster.pid。
您可以通过检查您的 data_directory 路径
cat /etc/postgresql/*/main/postgresql.conf
确认您的 data_directory 路径 - 然后发出以下命令。
rm /var/lib/postgresql/10/main/postmaster.pid
- 将 postgres 权限设置为 data_directory 路径。在我的例子中是/var/lib/postgresql/-
Honestly I am still looking for a (why we still need to set permission) where by default it is already have permission for postgres user.
sudo chmod 765 /var/run/postgresql
sudo chown postgres /var/run/postgresql
- 然后重启服务
sudo service postgresql restart
- 测试是否有效。
sudo -u postgres psql
注意:我使用的是Postgresql 10