使用Supervisord运行PostgreSQL

时间:2012-06-18 23:10:17

标签: postgresql ubuntu postgresql-9.1 supervisord

我想在Ubuntu 10.04上使用Supervisor运行PostgreSQL 9.1。目前,我使用init脚本手动启动PostgreSQL:

/etc/init.d/postgresql start

根据这篇文章:http://nicksergeant.com/using-postgresql-with-supervisor-on-ubuntu-1010/,我需要修改PostgreSQL配置,使其在TCP端口而不是Unix套接字上运行,以使PostgreSQL与Supervisor一起工作。

我对这种方法有两个问题:

  1. 考虑到这更像是黑客攻击,这样做是否有任何暗示(例如安全/权限,性能等)?

  2. 为什么我们不能在Supervisor配置中运行相同的init脚本postgresql?相反,如上面的链接所示,它会运行postmaster

  3. 更新

    感谢下面两个答案提供的有用建议,我已经为Supervisor设置了一个脚本来直接调用PostgreSQL:

    #!/bin/sh
    
    # This script is run by Supervisor to start PostgreSQL 9.1 in foreground mode
    
    if [ -d /var/run/postgresql ]; then
        chmod 2775 /var/run/postgresql
    else
        install -d -m 2775 -o postgres -g postgres /var/run/postgresql
    fi
    
    exec su postgres -c "/usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf"
    

    我还将config:/etc/postgresql/9.1/main/start.conf设置为manual,以便PostgreSQL在启动时不会自动启动(但是,我不清楚这个配置是否已加载)。然后我将postgres的Supervisor配置设置为:

    [program:postgres]
    user=root
    group=root
    command=/usr/local/bin/run_postgresql.sh
    autostart=true
    autorestart=true
    stderr_logfile=/home/www-data/logs/postgres_err.log
    stdout_logfile=/home/www-data/logs/postgres_out.log
    redirect_stderr=true
    stopsignal=QUIT
    

    现在,我可以通过执行supervisorctlstart postgres启动PostgreSQL,运行正常。 但是,在我发出stop postgres后,尽管supervisorctl声明postgres已停止,但服务器显然仍在运行,因为我可以将psql插入其中。

    我想知道这是Supervisor配置问题还是PostgreSQL问题。欢迎任何建议!

3 个答案:

答案 0 :(得分:3)

博客文章写得很糟糕。没有“TCP模式”:帖子建议的方法仍然会在Unix套接字上监听,只是在不同的目录中。帖子中的评论如“外部pid文件 - TCP模式不需要”是非常误导的。

postmaster是postgresql可执行文件的传统名称(用于区分主调度进程与后端从属进程)。一段时间以来,没有单独的可执行文件,现在它只是简单地安装为“postgres”。

假设Supervisor与qmail / daemontools supervise方案大体相似,那么让它运行一个设置目录和环境的脚本,然后执行,这完全是可能的(实际上很正常) postgres带有必要的参数(或者传播给包装器脚本的参数,这对于监督来说是不寻常的,但是当你有一个配置文件来放入参数时会更有意义。)

supervise工作的方式(我将继续假设“Supervisor”是相同的)是让管理程序进程按指定的方式运行子进程,并在退出时重新启动一个新的子进程。这是基于这样一种想法,即正在启动的进程是一个长期存在的守护程序进程,只有在出现严重错误时才会退出,并且只需重新启动它就是一个有效的修复程序。相比之下,/etc/init.d中的init脚本运行子进程并将其分离,并将控制权返回给调用者 - 如果子进程退出,则不会发生任何特殊情况,必须手动重新启动。如果你试图从监督中简单地运行/etc/init.d/postgresql start,那么它将继续产生postgresql守护进程,因为init脚本的返回将被解释为退出的守护程序进程,实际上它已经被启动和分离。 / p>

答案 1 :(得分:2)

为避免使用/etc/init.d脚本自动启动服务,postgresql 9.1的包提供了一个文件/etc/postgresql/9.1/main/start.conf,其中包含:

# Automatic startup configuration
# auto: automatically start/stop the cluster in the init script
# manual: do not start/stop in init scripts, but allow manual startup with
#         pg_ctlcluster
# disabled: do not allow manual startup with pg_ctlcluster (this can be easily
#           circumvented and is only meant to be a small protection for
#           accidents).

auto

这是要修改以避免自动启动的文件,而不是像博客帖子所暗示的那样移开/etc/init.d/postgresql

此外,更改unix套接字参数缺少/var/run/postgresql并不是最好的主意,因为它是与libpq链接​​的任何程序的默认值,因为它有&#39}使用适当的权限创建该目录没有困难,就像它在/usr/share/postgresql-common/init.d-functions中的包的开始序列所做的那样:

# create socket directory
if [ -d /var/run/postgresql ]; then
    chmod 2775 /var/run/postgresql
else
install -d -m 2775 -o postgres -g postgres /var/run/postgresql
fi

虽然默认值不应该导致问题,但请注意postmaster最终是保留在前景还是分叉并在后台运行是由postgresql.conf {{1}}参数控制的}。确保它已关闭。

答案 2 :(得分:1)

我试图让tomcat和postgres在主管下运行,并在此处找到一些提示:https://serverfault.com/questions/425132/controlling-tomcat-with-supervisor

这是我修改过的run_postgresql.sh,使用bash:

#!/bin/bash

# This script is run by Supervisor to start PostgreSQL 9.1 in foreground mode

function shutdown()
{
    echo "Shutting down PostgreSQL"
    pkill postgres
}

if [ -d /var/run/postgresql ]; then
    chmod 2775 /var/run/postgresql
else
    install -d -m 2775 -o postgres -g postgres /var/run/postgresql
fi

# Allow any signal which would kill a process to stop PostgreSQL
trap shutdown HUP INT QUIT ABRT KILL ALRM TERM TSTP

exec sudo -u postgres /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main --config-file=/etc/postgresql/9.1/main/postgresql.conf

使用此脚本,postgresql在supervisorctl stop postgres之后正确停止。