【问题标题】:google compute startup script starting postgresql谷歌计算启动脚本启动 postgresql
【发布时间】:2018-01-18 23:40:14
【问题描述】:

我有一个由实例模板设置的启动脚本,它为谷歌计算初始化服务器。安装 postgres 后,我手动调用它开始使用:

/etc/init.d/postgresql 启动

这成功完成了,但是当启动脚本运行时服务器没有监听 5432(postgres 没有启动,尽管服务启动调用成功完成)。启动完成后,我登录,我可以成功。任何人都知道为什么这在启动脚本中不起作用?我需要在启动过程中加载数据,所以我需要在初始化过程中启动 postgres。

【问题讨论】:

  • 您是在使用 CloudSQL for PostgreSQL 还是在 GCE VM 上安装 PostgreSQL,或者您只是想连接到 PostgreSQL 服务器?另外,能否分享一下你一直在使用的启动脚本和serial console的输出?
  • 好吧,我想我明白了。正在使用我认为稳定但显然有点太旧的旧版 debian。在我的模板中使用更新的图像,postgres 按预期出现。现在将关闭它。
  • 嗨,aao,您能否发布您的解决方案作为对这个问题的回答,这对社区有帮助,并且对将来遇到类似问题的人有更好的了解?

标签: google-cloud-platform google-compute-engine


【解决方案1】:

我使用较新的 debian 映像解决了

【讨论】:

    【解决方案2】:

    我遇到了和你一样的问题(在 GCE 启动脚本中安装 postgresql 导致软件包被安装,但服务器没有运行),我想我找到了根本原因。

    通常,postgresql-11 包应该在安装后启动 PostgreSQL 服务器。这是 postinst 脚本中的一个 sn-p:

    if [ "$1" = configure ]; then
        . /usr/share/postgresql-common/maintscripts-functions
    
        configure_version $VERSION "$2"
    fi
    

    看看/usr/share/postgresql-common/maintscripts-functions,我们看到:

    configure_version() {
        ...
        # reload systemd to let the generator pick up the new unit
        if [ -d /run/systemd/system ]; then
            systemctl daemon-reload
        fi
        invoke-rc.d postgresql start $VERSION # systemd: argument ignored, starts all versions
    }
    

    我的 debian 安装带有 init-system-helpers 版本“1.56+nmu1”,它在invoke-rc.d 中包含this bit of code

    # avoid deadlocks during bootup and shutdown from units/hooks
    # which call "invoke-rc.d service reload" and similar, since
    # the synchronous wait plus systemd's normal behaviour of
    # transactionally processing all dependencies first easily
    # causes dependency loops
    if ! systemctl --quiet is-active multi-user.target; then
      sctl_args="--job-mode=ignore-dependencies"
    fi
    case $saction in
      start|restart|try-restart)
          [ "$_state" != "LoadState=masked" ] || exit 0
          systemctl $sctl_args "${saction}" "${UNIT}" && exit 0
          ;;
    

    debian postgresql-11 软件包使用模板化的 systemd 单元。主要的称为postgresql.service,但这是一个虚拟服务,实际上并没有做任何事情。 PostgreSQL 服务器实际上是由一个名为 postgresql@11-main 的模板单元启动的,它通常与主服务一起启动,因为它有 ReloadPropagatedFrom=postgresql.service

    请注意,发生此问题时,主机已启动,但模板未启动:

    $ sudo systemctl status postgresql
    ● postgresql.service - PostgreSQL RDBMS
       Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
       Active: active (exited) since Fri 2021-04-02 05:40:48 UTC; 32min ago
     Main PID: 1663 (code=exited, status=0/SUCCESS)
        Tasks: 0 (limit: 4665)
       Memory: 0B
       CGroup: /system.slice/postgresql.service
    
    Apr 02 05:40:48 hubnext-west-r21r systemd[1]: Starting PostgreSQL RDBMS...
    Apr 02 05:40:48 hubnext-west-r21r systemd[1]: Started PostgreSQL RDBMS.
    
    $ sudo systemctl status postgresql@11-main
    ● postgresql@11-main.service - PostgreSQL Cluster 11-main
       Loaded: loaded (/lib/systemd/system/postgresql@.service; enabled-runtime; vendor preset: enabled)
       Active: inactive (dead)
    

    这是因为当指定--job-mode=ignore-dependencies 时,此链接将被忽略。

    GcE 启动脚本作为 systemd 单元运行,在 multi-user.target 启动之前启动:

    $ find /etc/systemd | grep startup
    /etc/systemd/system/multi-user.target.wants/google-startup-scripts.service
    

    因此,invoke-rc.d 注意到 systemctl --quiet is-active multi-user.target 为 false 并添加 --job-mode=ignore-dependencies,导致 PostgreSQL 服务器无法启动。

    一种可能的解决方法是在安装 postgres 后从您的启动脚本中显式运行 systemd start postgresql@11-main.service


    顺便说一句,我注意到 a recent commit(2020 年 11 月)更改了此 invoke-rc.d 行为,使其不再使用 --job-mode=ignore-dependencies。这将有助于避免这个问题。

    【讨论】:

      猜你喜欢
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      • 2017-09-28
      • 1970-01-01
      • 2018-01-14
      • 2020-10-11
      相关资源
      最近更新 更多