Postgresql12.3 不会在引导时启动,systemd

Postgresql12.3 wont start on boot, systemd

我从源代码安装了 Postgres 12.3,步骤如下(根据 this):

./configure --with-openssl --with-systemd
make
sudo make install

如果我从 postgres 用户的 pg_ctl 开始一切正常: pg_ctl -D $PGDATA -l /path/to/logfile

然后我尝试创建一个 systemd 服务,如 here 所述。 步骤:

  1. 创建文件 /etc/systemd/system/postgresql.service 内容为:
[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /path/to/pgdata
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target
  1. sudo systemctl enable postgresql.service
  2. 然后我重新启动我的机器。 重启后 Postgres 不可用。一些日志: sudo systemctl status postgresql.service
postgresql.service - PostgreSQL database server
   Loaded: loaded (/etc/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Fri 2020-06-05 03:23:32 MSK; 37s ago
     Docs: man:postgres(1)
  Process: 724 ExecStart=/usr/local/pgsql/bin/postgres -D /path/to/pgdata (code=exited, status=1/FAILURE)
 Main PID: 724 (code=exited, status=1/FAILURE)

Jun 05 03:23:31 ctsvc systemd[1]: Starting PostgreSQL database server...
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Main process exited, code=exited, status=1/FAILURE
Jun 05 03:23:32 ctsvc systemd[1]: Failed to start PostgreSQL database server.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Unit entered failed state.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Failed with result 'exit-code'.

journalctl -xe | grep postgres

-- Subject: Unit postgresql.service has begun start-up
-- Unit postgresql.service has begun starting up.
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.209 MSK [724] LOG:  starting PostgreSQL 12.3 on armv7l-unknown-linux-gnueabihf, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 32-bit
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] LOG:  could not bind IPv4 address "172.17.17.42": Cannot assign requested address
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] WARNING:  could not create listen socket for "172.17.17.42"
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.211 MSK [724] FATAL:  could not create any TCP/IP sockets
Jun 05 03:23:32 ctsvc postgres[724]: 2020-06-05 03:23:32.212 MSK [724] LOG:  database system is shut down
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Main process exited, code=exited, status=1/FAILURE
-- Subject: Unit postgresql.service has failed
-- Unit postgresql.service has failed.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Unit entered failed state.
Jun 05 03:23:32 ctsvc systemd[1]: postgresql.service: Failed with result 'exit-code'.
Jun 05 03:24:09 ctsvc sudo[1602]:   user1 : TTY=pts/0 ; PWD=/home/user1 ; USER=root ; COMMAND=/bin/systemctl status postgresql.service

netstat -tnl | grep "5432" - 什么都不显示。 之后我可以手动 运行 这个服务: sudo systemctl status postgresql.service

● postgresql.service - PostgreSQL database server
   Loaded: loaded (/etc/systemd/system/postgresql.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-06-05 03:30:57 MSK; 8s ago
     Docs: man:postgres(1)
 Main PID: 1681 (postgres)
    Tasks: 8 (limit: 4915)
   CGroup: /system.slice/postgresql.service
           ├─1681 /usr/local/pgsql/bin/postgres -D /path/to/pgdata
           ├─1683 postgres: checkpointer   
           ├─1684 postgres: background writer   
           ├─1685 postgres: walwriter   
           ├─1686 postgres: autovacuum launcher   
           ├─1687 postgres: stats collector   
           ├─1688 postgres: logical replication launcher   
           └─1693 postgres: postgres postgres 172.17.17.40(53600) idle

Jun 05 03:30:56 ctsvc systemd[1]: Starting PostgreSQL database server...
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.006 MSK [1681] LOG:  starting PostgreSQL 12.3 on armv7l-unknown-linux-gnueabihf, compiled b
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.007 MSK [1681] LOG:  listening on IPv4 address "172.17.17.42", port 5432
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.032 MSK [1681] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.424 MSK [1682] LOG:  database system was shut down at 2020-06-05 02:59:03 MSK
Jun 05 03:30:57 ctsvc postgres[1681]: 2020-06-05 03:30:57.725 MSK [1681] LOG:  database system is ready to accept connections
Jun 05 03:30:57 ctsvc systemd[1]: Started PostgreSQL database server.
netstat -tnl | grep '5432'
tcp        0      0 172.17.17.42:5432       0.0.0.0:*               LISTEN

在我的 postgresql.conf 我有以下内容:

# - Connection Settings -
listen_addresses = '172.17.17.42'
port = 5432
max_connections = 100

如果有帮助:Postgres 运行s on Cubietruck with Armbian。

uname -a
Linux ctsvc 4.19.62-sunxi #5.92 SMP Wed Jul 31 22:07:23 CEST 2019 armv7l GNU/Linux

在我的系统中,没有更多进程在启动时尝试绑定此端口。据我了解,服务本身和 Postgresql 一切都很好。然而,在启动过程中发生了一些奇怪的事情,但我不明白如何找出这种行为的原因。 提前致谢。

最后我的文件 /etc/systemd/system/postgresql.service 看起来像这样:

[Unit]
Description=PostgreSQL database server
Documentation=man:postgres(1)
Wants=network-online.target
After=network.target network-online.target

[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /path/to/pgdata
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=0

[Install]
WantedBy=multi-user.target

感谢 Laurenz Albe ,我在 Unit 部分添加了以下内容:

Wants=network-online.target
After=network.target network-online.target

确保PG启动前网络完全正常运行。重启后这个PG运行正确