Systemd

postgresql.service 如何知道要啟動哪些 postgresql 實例?

  • January 24, 2022

我已經在 ubuntu 16.04 上安裝了 postgres 9.5,它創建postgresql.servicepostgresql@.service.

我知道postgresql.service生成所有啟用的 postgres 實例,我可以使用模板文件呼叫特定實例,postgresql@9.5-mainpostgresql@.service我看不到實例字元串(由模板中的 %i 或 %I 表示)的任何地方被路過postgresql.service

如何postgresql.service知道啟用了哪些實例,以及如何將它們傳遞給 systemd 模板文件?

要回答這個問題,首先要檢查有問題的兩個文件的內容。如果您不確定在哪裡可以找到它們,您可以在包內容中搜尋systemd文件:

 dpkg -L postgresql-common| grep systemd

通過查看postgresql.service文件,您可以看到您根本沒有做太多事情:

# systemd service for managing all PostgreSQL clusters on the system. This
# service is actually a systemd target, but we are using a service since
# targets cannot be reloaded.

[Unit]
Description=PostgreSQL RDBMS

[Service]
Type=oneshot
ExecStart=/bin/true
ExecReload=/bin/true
RemainAfterExit=on

[Install]
WantedBy=multi-user.target

從評論中,我們了解到該文件被用作 systemd “目標”。轉到模板文件:

# 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
ConditionPathExists=/etc/postgresql/%I/postgresql.conf
PartOf=postgresql.service
ReloadPropagatedFrom=postgresql.service
Before=postgresql.service

[Service]
Type=forking
# @: use "postgresql@%i" as process name
ExecStart=@/usr/bin/pg_ctlcluster postgresql@%i --skip-systemctl-redirect %i start
ExecStop=/usr/bin/pg_ctlcluster --skip-systemctl-redirect -m fast %i stop
ExecReload=/usr/bin/pg_ctlcluster --skip-systemctl-redirect %i reload
PIDFile=/var/run/postgresql/%i.pid
SyslogIdentifier=postgresql@%i
# prevent OOM killer from choosing the postmaster (individual backends will
# reset the score to 0)
OOMScoreAdjust=-900
# restarting automatically will prevent "pg_ctlcluster ... stop" from working,
# so we disable it here. Also, the postmaster will restart by itself on most
# problems anyway, so it is questionable if one wants to enable external
# automatic restarts.
#Restart=on-failure
# (This should make pg_ctlcluster stop work, but doesn't:)
#RestartPreventExitStatus=SIGINT SIGTERM

[Install]
WantedBy=multi-user.target

有趣的指令是:

PartOf=postgresql.service
ReloadPropagatedFrom=postgresql.service

如果您不確定在哪裡可以找到systemd指令的文件,您可以檢查:man systemd.directives. 從那裡,我們在man systemd.unit.

當您啟用服務時,您最大的線索是:

sudo systemctl enable postgresql@9.6
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql@9.6.service → /lib/systemd/system/postgresql@.service.

把它們放在一起:

  • 符號連結是如何systemd知道在伺服器啟動時啟動 PostgreSQL 9.6。
  • PartOf=ReloadPropagatedFrom=指令確保服務上的、 和最終stop應用於start所有相關的已安裝 PostgreSQL 實例。restart``reload``postgresql

引用自:https://unix.stackexchange.com/questions/436802