代码之家  ›  专栏  ›  技术社区  ›  malisper

Postgres systemd单元文件如何决定运行哪个版本的Postgres?

  •  2
  • malisper  · 技术社区  · 7 年前

    这主要是出于好奇。我正在浏览Postgres systemd单元文件,以了解systemd可以做些什么。Postgres有两个系统单元文件。用于代替systemd目标的一种:

    # 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
    

    第二个单元文件是由Postgres版本参数化的模板。它通过使用指定将在其他单元文件之前运行 Before=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
    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
    

    我不明白的是,当我运行时,systemd是如何决定要运行哪个版本的Postgres的 systemctl start postgresql . 当我看到 postgresql.service

    > systemctl list-dependencies postgresql
    
    postgresql.service
    ● ├─postgresql@9.5-main.service
    ● ├─postgresql@9.5-main.service
    ● ├─system.slice
    ● └─sysinit.target
    ●   ├─apparmor.service
    ●   ├─brltty.service
    ●   ├─console-setup.service
    ●   ├─dev-hugepages.mount
    ●   ├─dev-mqueue.mount
    ●   ├─friendly-recovery.service
    ●   ├─keyboard-setup.service
    ●   ├─kmod-static-nodes.service
    ●   ├─lvm2-lvmetad.socket
    ●   ├─lvm2-lvmpolld.socket
    ●   ├─lvm2-monitor.service
    ●   ├─plymouth-read-write.service
    ●   ├─plymouth-start.service
    ●   ├─proc-sys-fs-binfmt_misc.automount
    ●   ├─resolvconf.service
    ●   ├─setvtrgb.service
    ●   ├─sys-fs-fuse-connections.mount
    ●   ├─sys-kernel-config.mount
    

    我找不到任何文件指定应该使用Postgres 9.5。没有任何其他Postgres单元文件,也没有任何其他文件提及Postgres。这是在带有systemd 229和Postgres 9.5(通过安装)的Ubuntu 16.04上 sudo apt-get install postgresql

    1 回复  |  直到 7 年前
        1
  •  3
  •   papey    7 年前

    发件人: https://www.freedesktop.org/software/systemd/man/systemd.unit.html

    "%i"    Instance name   For instantiated units: this is the string between the "@" character and the suffix of the unit name.
    

    文件名为postgres@9.3-main.service所以 %i = 9.3-main

    更新:

    file /run/systemd/generator/postgresql.service.wants/postgresql@9.4-main.service
    /run/systemd/generator/postgresql.service.wants/postgresql@9.4-main.service: symbolic link to /lib/systemd/system/postgresql@.service