summaryrefslogtreecommitdiffstats
path: root/postgresql.init.in
blob: aadd3dd2b74126a00202c1a3fcc26dd8a1ba8541 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/bin/bash
#
# @NAME_SERVICE@
#
# This is the init script for starting up the PostgreSQL server.
#
# This script is slightly unusual in that the name of the daemon (postmaster)
# is not the same as the name of the subsystem (postgresql)
#
# chkconfig: - 64 36
# description: PostgreSQL database server.
# processname: postmaster
# pidfile: /var/run/postmaster.PORT.pid

### BEGIN INIT INFO
# Provides: @NAME_SERVICE@
# Required-Start: $local_fs $remote_fs $network $named $syslog $time
# Required-Stop: $local_fs $remote_fs $network $named $syslog $time
# Short-Description: start and stop PostgreSQL @PGVERSION@ server
# Description: PostgreSQL database server
### END INIT INFO

# PGVERSION is the full package version, e.g., 9.0.2
# Note: the specfile inserts the correct value during package build
PGVERSION=@PGVERSION@

# PGMAJORVERSION is major version, e.g., 9.0 (this should match PG_VERSION)
PGMAJORVERSION=@PGMAJORVERSION@

# PGDOCDIR is the directory containing the package's documentation
# Note: the specfile inserts the correct value during package build

# Distribution README file
README_DIST=@README_DIST@

# Source function library.
. /etc/rc.d/init.d/functions

# Get network config.
. /etc/sysconfig/network

# postgresql-setup library
. "@rawpkgdatadir@/library.sh"

@SCL_SOURCE@

# Find the name of the script
NAME=`basename $0`
if [ ${NAME:0:1} = "S" -o ${NAME:0:1} = "K" ]
then
	NAME=${NAME:3}
fi

SU_POSTGRES="@SU_POSTGRES@"

# Set defaults for configuration variables
PGENGINE=@bindir@

# Only default system service has default PGDATA set.  This allows us to catch
# admin's mistake of not creating /etc/sysconfig/psql/$NAME configuration file
# and just hard/symlinking default init script.  That way we can avoid (inside
# postgresql-check-db-dir) runnnig "secondary" server against "default" data
# directory.
if test "@NAME_SERVICE@" = "$NAME"; then
    PGDATA=@PGDATADIR@
fi

PGLOG=@POSTGRES_HOMEDIR@/pgstartup-@NAME_SERVICE@.log

# Value to set as postmaster process's oom_adj
PG_OOM_ADJ=-17

# Override defaults from /etc/sysconfig/pgsql if file is present
[ -f /etc/sysconfig/pgsql/${NAME} ] && . /etc/sysconfig/pgsql/${NAME}

export PGDATA
export PGPORT

lockfile="/var/lock/subsys/${NAME}"

# Ideally, we should use $PGDATA/postmaster.pid.  It apparently works, but to
# be honest I'm not sure about consequences.  Most probably 'service status'
# would not work for non-root/non-postgres users.  TODO?
pidfile="/var/run/${NAME}.pid"

script_result=0

start()
{
	[ -x "$PGENGINE/postgres" ] || exit 5

	PSQL_START=$"Starting ${NAME} service: "

	# Make sure startup-time log file is valid
	if [ ! -e "$PGLOG" -a ! -h "$PGLOG" ]
	then
		touch "$PGLOG" || exit 4
		chown postgres:postgres "$PGLOG"
		chmod go-rwx "$PGLOG"
		[ -x /sbin/restorecon ] && /sbin/restorecon "$PGLOG"
	fi

    @libexecdir@/postgresql-check-db-dir "$NAME" || {
        echo_failure
        echo
        exit 1
    }

	echo -n "$PSQL_START"
	test x"$PG_OOM_ADJ" != x && echo "$PG_OOM_ADJ" > /proc/self/oom_adj

    # Note that this does not fail/exit the 'service start' if the postmaster
    # is already running.  We should probably 'status' first and start only if
    # postmaster is down.  This just unnecessarily wastes time and generates
    # too much (false) rush in $PGLOG.
    run_cmd_as_dbadmin \
            "$PGENGINE/postgres ${PGPORT+-o "-p $PGPORT"} \
                    -D '$PGDATA' ${PGOPTS} &"               \
            "$PGLOG" "$PGLOG"

	# TODO: parametrize
	sleep 2

	pid=`head -n 1 "$PGDATA/postmaster.pid" 2>/dev/null`
	if [ "x$pid" != x ]
	then
		success "$PSQL_START"
		touch "$lockfile"
		echo $pid > "$pidfile"
		echo
	else
		failure "$PSQL_START"
		echo
		script_result=1
	fi
}

stop()
{
	echo -n $"Stopping ${NAME} service: "
	if [ -e "$lockfile" ]
	then
        run_cmd_as_dbadmin \
                "$PGENGINE/pg_ctl stop -D '$PGDATA' -s -m fast" \
                /dev/null /dev/null
	    ret=$?
	    if [ $ret -eq 0 ]
	    then
		echo_success
		rm -f "$pidfile"
		rm -f "$lockfile"
	    else
		echo_failure
		script_result=1
	    fi
	else
	    # not running; per LSB standards this is "ok"
	    echo_success
	fi
	echo
}

restart(){
    stop
    start
}

condrestart(){
    [ -e "$lockfile" ] && restart || :
}

reload()
{
    $SU_POSTGRES -c "@SCL_SOURCE@ $PGENGINE/pg_ctl reload -D '$PGDATA' -s" > /dev/null 2>&1 < /dev/null
}

__single_comand()
{
    local msg="$1"
    shift
    echo $"$msg ($@)"
    "$@" && success || failure || script_result=1
    echo
}


initdb()
{
    __single_comand $"Initializing database" \
        @NAME_BINARYBASE@-setup --initdb "$NAME" "$@"
}


upgrade()
{
    __single_comand $"Upgrading database" \
        @NAME_BINARYBASE@-setup --upgrade "$NAME" "$@"
}


# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status -p "$pidfile" postgres
	script_result=$?
	;;
  restart)
	restart
	;;
  condrestart|try-restart)
	condrestart
	;;
  reload)
	reload
	;;
  force-reload)
	restart
	;;
  initdb)
    shift
    initdb "$@"
    ;;
  upgrade)
    shift
    upgrade "$@"
    ;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|initdb|upgrade}"
	exit 2
esac

exit $script_result