Postgresql 10 和 Ubuntu - 无法启动 postgresql 服务器

Jin*_*Lim 2 postgresql ubuntu

有一天,我的 Postgresql 服务器停止工作。检查日志。它以某种方式关闭了。

\n\n
root@ip_address:/# tail /var/log/postgresql/postgresql-10-main.log\n2020-02-19 06:47:49.215 CET [23497] LOG:  received smart shutdown request\n2020-02-19 06:47:49.477 CET [23497] LOG:  worker process: logical replication launcher (PID 23512) exited with exit code 1\n2020-02-19 06:47:49.482 CET [23507] LOG:  shutting down\n2020-02-19 06:47:49.546 CET [23497] LOG:  database system is shut down\n
Run Code Online (Sandbox Code Playgroud)\n\n

当我跑步时,

\n\n
root@ip_address:/# psql\npsql: could not connect to server: No such file or directory\n    Is the server running locally and accepting\n    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?\n
Run Code Online (Sandbox Code Playgroud)\n\n

它抱怨没有文件和目录。所以我检查了我是否postgresql跑步。

\n\n
root@ip_address:/# systemctl status postgresql\n\xe2\x97\x8f postgresql.service - PostgreSQL RDBMS\n   Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)\n   Active: active (exited) since Sun 2020-03-08 16:19:24 CET; 26min ago\n  Process: 30136 ExecStart=/bin/true (code=exited, status=0/SUCCESS)\n Main PID: 30136 (code=exited, status=0/SUCCESS)\n\nMar 08 16:19:24 vps584959 systemd[1]: Starting PostgreSQL RDBMS...\nMar 08 16:19:24 vps584959 systemd[1]: Started PostgreSQL RDBMS.\n
Run Code Online (Sandbox Code Playgroud)\n\n

它正在运行。但是,如果我检查 postgresql 集群。

\n\n
root@ip_address:/# pg_lsclusters\nVer Cluster Port Status Owner    Data directory              Log file\n10  main    5432 down   postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log\n
Run Code Online (Sandbox Code Playgroud)\n\n

它是向下的

\n\n

所以我尝试了

\n\n
root@ip_address:/# pg_ctlcluster 10 main start\n Error: Config owner (deploy:1003) and data owner (postgres:114) do not match, and config owner is not root\n
Run Code Online (Sandbox Code Playgroud)\n\n

我无法让它发挥作用。然后我尝试了。

\n\n
sudo chown -R deploy:postgres /var/lib/postgresql/10/ && sudo chmod -R u=rwX,go= /var/lib/postgresql/10/\n
Run Code Online (Sandbox Code Playgroud)\n\n

再试一次。

\n\n
root@ip_address:/# pg_ctlcluster 10 main start\nJob for postgresql@10-main.service failed because the service did not take the steps required by its unit configuration.\nSee "systemctl status postgresql@10-main.service" and "journalctl -xe" for details.\n
Run Code Online (Sandbox Code Playgroud)\n\n
root@ip_address:/# systemctl status postgresql@10-main.service\n\xe2\x97\x8f postgresql@10-main.service - PostgreSQL Cluster 10-main\n   Loaded: loaded (/lib/systemd/system/postgresql@.service; indirect; vendor preset: enabled)\n   Active: failed (Result: protocol) since Sun 2020-03-08 16:59:53 CET; 2min 52s ago\n  Process: 31635 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 10-main start (code=exited, status=1/FAILURE)\n Main PID: 23497 (code=exited, status=0/SUCCESS)\n\nMar 08 16:59:53 vps584959 systemd[1]: Starting PostgreSQL Cluster 10-main...\nMar 08 16:59:53 vps584959 postgresql@10-main[31635]: Error: /usr/lib/postgresql/10/bin/pg_ctl /usr/lib/postgresql/10/bin/pg_ctl start -D /var/lib/postgresql/10/main -l /var/log/postgre\nMar 08 16:59:53 vps584959 systemd[1]: postgresql@10-main.service: Can\'t open PID file /var/run/postgresql/10-main.pid (yet?) after start: No such file or directory\nMar 08 16:59:53 vps584959 systemd[1]: postgresql@10-main.service: Failed with result \'protocol\'.\nMar 08 16:59:53 vps584959 systemd[1]: Failed to start PostgreSQL Cluster 10-main.\n
Run Code Online (Sandbox Code Playgroud)\n\n

不知道还能做什么。有人遇到同样的问题吗?

\n\n

更多信息。

\n\n
root@ip_address:/var/run/postgresql# ls\ntotal 0\ndrwxrwsr-x  3 postgres postgres   60 Feb 19 06:47 .\ndrwxr-xr-x 28 root     root     1060 Mar  8 13:58 ..\ndrwxr-s---  2 postgres postgres   40 Feb 19 06:47 10-main.pg_stat_tmp\n
Run Code Online (Sandbox Code Playgroud)\n\n
root@vps584959:~# journalctl -xe\nMar 08 17:46:07 vps584959 sudo[2154]: root : TTY=pts/0 ; PWD=/root ; USER=root ; COMMAND=/bin/systemctl start postgresql@10-main\nMar 08 17:46:07 vps584959 sudo[2154]: pam_unix(sudo:session): session opened for user root by root(uid=0)\nMar 08 17:46:07 vps584959 systemd[1]: Starting PostgreSQL Cluster 10-main...\n-- Subject: Unit postgresql@10-main.service has begun start-up\n-- Defined-By: systemd\n-- Support: http://www.ubuntu.com/support\n--\n-- Unit postgresql@10-main.service has begun starting up.\nMar 08 17:46:07 vps584959 postgresql@10-main[2157]: Error: Config owner (deploy:1003) and data owner (root:0) do not match, and config owner is not root\nMar 08 17:46:07 vps584959 systemd[1]: postgresql@10-main.service: Can\'t open PID file /var/run/postgresql/10-main.pid (yet?) after start: No such file or directory\nMar 08 17:46:07 vps584959 systemd[1]: postgresql@10-main.service: Failed with result \'protocol\'.\nMar 08 17:46:07 vps584959 systemd[1]: Failed to start PostgreSQL Cluster 10-main.\n-- Subject: Unit postgresql@10-main.service has failed\n-- Defined-By: systemd\n-- Support: http://www.ubuntu.com/support\n--\n-- Unit postgresql@10-main.service has failed.\n--\n-- The result is RESULT.\nMar 08 17:46:07 vps584959 sudo[2154]: pam_unix(sudo:session): session closed for user root\nMar 08 17:46:08 vps584959 sshd[2152]: Invalid user ftp1 from x.x.x.x port 57060\nMar 08 17:46:08 vps584959 sshd[2152]: pam_unix(sshd:auth): check pass; user unknown\nMar 08 17:46:08 vps584959 sshd[2152]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=x.x.x.x\nMar 08 17:46:09 vps584959 sshd[2152]: Failed password for invalid user ftp1 from 159.89.196.75 port 57060 ssh2\nMar 08 17:46:10 vps584959 sshd[2152]: Received disconnect from x.x.x.x port 57060:11: Bye Bye [preauth]\nMar 08 17:46:10 vps584959 sshd[2152]: Disconnected from invalid user ftp1 159.89.196.75 port 57060 [preauth]\nMar 08 17:46:11 vps584959 sshd[2150]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=x.x.x.x  user=root\nMar 08 17:46:12 vps584959 sshd[2159]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=x.x.x.x  user=root\nMar 08 17:46:13 vps584959 sshd[2150]: Failed password for root from xx.xx.xx.xx port 20408 ssh2\n
Run Code Online (Sandbox Code Playgroud)\n\n

更新\n-仍然不起作用。

\n\n
root@myserver:~# chown -R postgres:postgres /etc/postgresql/10/main/\nroot@myserver:~#\nroot@myserver:~#\nroot@myserver:~# pg_ctlcluster 10 main start\nJob for postgresql@10-main.service failed because the service did not take the steps required by its unit configuration.\nSee "systemctl status postgresql@10-main.service" and "journalctl -xe" for details.\nroot@myserver:~# systemctl status postgresql@10-main.service\n\xe2\x97\x8f postgresql@10-main.service - PostgreSQL Cluster 10-main\n   Loaded: loaded (/lib/systemd/system/postgresql@.service; indirect; vendor preset: enabled)\n   Active: failed (Result: protocol) since Thu 2020-03-12 00:09:43 CET; 7s ago\n  Process: 23767 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 10-main start (code=exited, status=1/FAILURE)\n\nroot@vps584959:~# systemctl status postgresql@10-main.service\n\xe2\x97\x8f postgresql@10-main.service - PostgreSQL Cluster 10-main\n   Loaded: loaded (/lib/systemd/system/postgresql@.service; indirect; vendor preset: enabled)\n   Active: failed (Result: protocol) since Thu 2020-03-12 00:09:43 CET; 11min ago\n  Process: 23767 ExecStart=/usr/bin/pg_ctlcluster --skip-systemctl-redirect 10-main start (code=exited, status=1/FAILURE)\n\n
Run Code Online (Sandbox Code Playgroud)\n\n

更新2

\n\n
root@vps584959:~# journalctl -xe\n--\n-- Unit UNIT has finished starting up.\n--\n-- The start-up result is RESULT.\nMar 14 13:55:16 vps584959 systemd[31170]: Startup finished in 171ms.\n-- Subject: User manager start-up is now complete\n-- Defined-By: systemd\n-- Support: http://www.ubuntu.com/support\n--\n-- The user manager instance for user 0 has been started. All services queued\n-- for starting have been started. Note that other services might still be starting\n-- up or be started at any later time.\n--\n-- Startup of the manager took 171927 microseconds.\nMar 14 13:55:17 vps584959 sshd[31156]: Failed password for root from 49.88.112.111 port 29693 ssh2\nMar 14 13:55:18 vps584959 sshd[31156]: Received disconnect from 49.88.112.111 port 29693:11:  [preauth]\nMar 14 13:55:18 vps584959 sshd[31156]: Disconnected from authenticating user root 49.88.112.111 port 29693 [preauth]\nMar 14 13:55:18 vps584959 sshd[31156]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=49.88.112.111  user=root\nMar 14 13:55:33 vps584959 sshd[31363]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=134.17.94.237  user=root\nMar 14 13:55:35 vps584959 sshd[31363]: Failed password for root from 134.17.94.237 port 3684 ssh2\nMar 14 13:55:35 vps584959 sshd[31363]: Received disconnect from 134.17.94.237 port 3684:11: Bye Bye [preauth]\nMar 14 13:55:35 vps584959 sshd[31363]: Disconnected from authenticating user root 134.17.94.237 port 3684 [preauth]\nMar 14 13:55:43 vps584959 systemd[1]: Starting PostgreSQL Cluster 10-main...\n-- Subject: Unit postgresql@10-main.service has begun start-up\n-- Defined-By: systemd\n-- Support: http://www.ubuntu.com/support\n--\n-- Unit postgresql@10-main.service has begun starting up.\nMar 14 13:55:43 vps584959 postgresql@10-main[31373]: Error: /usr/lib/postgresql/10/bin/pg_ctl /usr/lib/postgresql/10/bin/pg_ctl start -D /var/lib/postgresql/10/main -l /var/log/p\nMar 14 13:55:43 vps584959 postgresql@10-main[31373]: 2020-03-14 13:55:43.696 CET [31378] FATAL:  private key file "/etc/ssl/private/ssl-cert-snakeoil.key" must be owned by the da\nMar 14 13:55:43 vps584959 postgresql@10-main[31373]: 2020-03-14 13:55:43.698 CET [31378] LOG:  database system is shut down\nMar 14 13:55:43 vps584959 postgresql@10-main[31373]: pg_ctl: could not start server\nMar 14 13:55:43 vps584959 postgresql@10-main[31373]: Examine the log output.\nMar 14 13:55:43 vps584959 systemd[1]: postgresql@10-main.service: Can\'t open PID file /var/run/postgresql/10-main.pid (yet?) after start: No such file or directory\nMar 14 13:55:43 vps584959 systemd[1]: postgresql@10-main.service: Failed with result \'protocol\'.\nMar 14 13:55:43 vps584959 systemd[1]: Failed to start PostgreSQL Cluster 10-main.\n-- Subject: Unit postgresql@10-main.service has failed\n-- Defined-By: systemd\n-- Support: http://www.ubuntu.com/support\n--\n-- Unit postgresql@10-main.service has failed.\n--\n-- The result is RESULT.\n
Run Code Online (Sandbox Code Playgroud)\n\n

和,

\n\n
root@vps584959:~# cat /var/log/postgresql/postgresql-10-main.log\n.\n.\n2020-03-12 00:09:43.609 CET [23773] FATAL:  private key file "/etc/ssl/private/ssl-cert-snakeoil.key" must be owned by the database user or root\n2020-03-12 00:09:43.611 CET [23773] LOG:  database system is shut down\npg_ctl: could not start server\nExamine the log output.\n2020-03-14 13:55:43.696 CET [31378] FATAL:  private key file "/etc/ssl/private/ssl-cert-snakeoil.key" must be owned by the database user or root\n2020-03-14 13:55:43.698 CET [31378] LOG:  database system is shut down\npg_ctl: could not start server\nExamine the log output.\nroot@vps584959:~#\n
Run Code Online (Sandbox Code Playgroud)\n

Dan*_*ité 6

systemctl status postgresql对于检查 postgresql 实例没有用,因为它只是一个伞式服务。你想要systemctl status postgresql@10-main.service在你的情况下。当实例关闭时,它会正确地将状态显示为非活动状态。

正如开头所解释的/lib/systemd/system/postgresql@.service

$ head /lib/systemd/system/postgresql@.service
# systemd service template for PostgreSQL clusters. The actual instances will
# be called "postgresql@version-cluster", e.g. "postgresql@9.3-main". The
# variable %i expands to "version-cluster", %I expands to "version/cluster".
# (%I breaks for cluster names containing dashes.)

[Unit]
Description=PostgreSQL Cluster %i
AssertPathExists=/etc/postgresql/%I/postgresql.conf
RequiresMountsFor=/etc/postgresql/%I /var/lib/postgresql/%I
PartOf=postgresql.service
Run Code Online (Sandbox Code Playgroud)

至于服务器无法启动的原始问题,是因为postgres应该拥有配置文件和数据目录以及其中的所有内容。由于某种原因,您/某人/某物将这些文件重新分配给deploy用户,这不能与 Ubuntu/Debian 中设置 PosgreSQL 的方式一起工作。只需保留最初设置的权限和所有者即可。


有关 SSL 密钥文件的 Pper 注释:以下错误表明必须在 SSL 私钥文件上恢复权限和所有权:

PostgreSQL 错误:

FATAL:  private key file "/etc/ssl/private/ssl-cert-snakeoil.key" must be owned by the database user or root
Run Code Online (Sandbox Code Playgroud)

恢复所有权和权限的命令:

$ sudo chown root:ssl-cert /etc/ssl/private/ssl-cert-snakeoil.key
$ sudo chmod 640 /etc/ssl/private/ssl-cert-snakeoil.key
Run Code Online (Sandbox Code Playgroud)