Starting PostgreSQL with Supervisord

I want to run PostgreSQL 9.1 using Supervisor on Ubuntu 10.04. At the moment, I manually start PostgreSQL using the init script:

/etc/init.d/postgresql start

According to this post: http://nicksergeant.com/using-postgresql-with-supervisor-on-ubuntu-1010/ , I need to change the configuration of PostgreSQL so that it runs on a TCP port instead of a Unix socket to force PostgreSQL work with Supervisor.

I have two questions regarding this approach:

  • Given that this has more to do with the hack, are there any implications (e.g. security / permissions, performance, etc.)?

  • Why can't we run the same init script postgresqlin the Supervisor configuration? Instead, as shown in the link above, does it launch postmaster?

UPDATE

Thanks to the helpful suggestions from both answers below, I setup a script for Supervisor to call PostgreSQL directly:

#!/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"

I also set config: /etc/postgresql/9.1/main/start.confto manualso that PostgreSQL does not start automatically at boot (however, it is not clear to me if this config is loaded). And then I configure the Supervisor configuration for postgres as:

[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

So now I can start PostgreSQL in supervisorctlby doing start postgresthat which works fine. However, after I issued stop postgres, although it supervisorctldeclares that postgres has stopped, the server seems to be still working, since I can use psql.

, Supervisor PostgreSQL. !

+5
3

. " TCP": Unix . , " pid - TCP", .

postmaster - postgresql ( ). , "postgres".

, Supervisor qmail/daemontools supervise, ( , ), script, , execs postgres ( , script, , , ).

supervise ( , "" - ) , , , , . , - , , - , . , , /etc/init.d, , - , , . /etc/init.d/postgresql start , postgresql, init script , , .

+3

/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, , /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 forks , silent_mode postgresql.conf. , .

+2

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

script postgresql supervisorctl stop postgres.

+1
source

All Articles