aboutsummaryrefslogtreecommitdiff
blob: d0ef87c7a7589a1334556aec5acdae795c21302e (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
#!/sbin/runscript
# Copyright 1999-2010 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/dev-db/postgresql-server/files/postgresql.init,v 1.1 2010/06/04 14:18:52 patrick Exp $

opts="${opts} reload"

depend() {
	use net
	provide postgresql
	provide postgresql-8.2
}

checkconfig() {
	if [ ! -d $DATA_DIR ] ; then
		eerror "Directory not found: $DATA_DIR"
		eerror "Please make sure that DATA_DIR points to the right path."
		eerror "You can run 'emerge --config dev-db/postgresql-server:8.2' to setup a new"
		eerror "database cluster."
		return 1
	elif [ ! -f ${PGDATA%/}/postgresql.conf ] ; then
		eerror "File not found: ${PGDATA%/}/postgresql.conf"
		eerror "You may need to run:"
		eerror "cp ${DATA_DIR%/}/postgresql.conf ${PGDATA%/}/postgresql.conf"
		return 1
	elif [ ! -f ${PGDATA%/}/pg_hba.conf ] ; then
		eerror "File not found: ${PGDATA%/}/pg_hba.conf"
		eerror "You may need to run:"
		eerror "cp ${DATA_DIR%/}/pg_hba.conf ${PGDATA%/}/pg_hba.conf"
		return 1
	elif [ ! -f ${PGDATA%/}/pg_ident.conf ] ; then
		eerror "File not found: ${PGDATA%/}/pg_ident.conf"
		eerror "You may need to run:"
		eerror "cp ${DATA_DIR%/}/pg_ident.conf ${PGDATA%/}/pg_ident.conf"
		return 1
	elif [ -e /var/run/postgresql/.s.PGSQL.${PGPORT} ] ; then
		eerror "Socket conflict."
		eerror "A server is already listening on:"
		eerror "/var/run/postgresql/.s.PGSQL.${PGPORT}."
		eerror "Change PGPORT to listen on a different socket."
		return 1
	fi
}

start() {
	checkconfig || return 1

	ebegin "Starting PostgreSQL"

	if [ -f ${DATA_DIR%/}/postmaster.pid ] ; then
		rm -f ${DATA_DIR%/}/postmaster.pid
	fi

	local retval

	su -l postgres \
		-c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
		/usr/lib/postgresql-8.2/bin/pg_ctl \
		start ${WAIT_FOR_START} -s -D ${DATA_DIR} -o \
		'-D ${PGDATA} --data-directory=${DATA_DIR} --silent-mode=true ${PGOPTS}'"
	retval=$?

	if [ $retval -ne 0 ] ; then
		eend $retval
		return $retval
	fi

	# The following is to catch the case of an already running server
	# in which pg_ctl doesn't know to which server it connected to and
	# falsely reports the server as 'up'
	if [ ! -f ${DATA_DIR}/postmaster.pid ] ; then
		eerror "The PID file doesn't exist but pg_ctl reported a running server."
		eerror "Please check whether there is another server running on the same port or read the log-file."
		eend 1
		return 1
	fi

	eend $?
}

stop() {
	ebegin "Stopping PostgreSQL (this can take up to 90 seconds)"

	local retval

	if [ "${NICE_QUIT}" != "NO" ] ; then
		su -l postgres \
			-c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
			/usr/lib/postgresql-8.2/bin/pg_ctl \
			stop ${WAIT_FOR_STOP} -s -D ${DATA_DIR} -m smart"
		retval=$?

		if [ $retval -eq 0 ] ; then
			eend $retval
			return $retval
		fi

		ewarn "Shutting down the server gracefully failed."
		ewarn "Probably because some clients did not disconnect within ${NICE_TIMEOUT} seconds."
	else
		ewarn "NICE_QUIT disabled."
		ewarn "You really should have it enabled."
	fi

	if [ "${RUDE_QUIT}" != "NO" ] ; then
		ewarn "RUDE_QUIT enabled."
		ewarn "Going to shutdown the server anyway."

		su -l postgres \
			-c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
			/usr/lib/postgresql-8.2/bin/pg_ctl \
			stop ${WAIT_FOR_STOP} -s -D ${DATA_DIR} -m fast"
		retval=$?

		if [ $retval -eq 0 ] ; then
			eend $retval
			return $retval
		fi

		eerror "Failed to shutdown server."
	else
		ewarn "RUDE_QUIT disabled."
	fi

	if [ "${FORCE_QUIT}" = "YES" ] ; then
		ewarn "FORCE_QUIT enabled."
		ewarn "Forcing server to shutdown."
		ewarn "A recover-run will be executed on the next startup."

		su -l postgres \
			-c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
			/usr/lib/postgresql-8.2/bin/pg_ctl \
			stop ${WAIT_FOR_STOP} -s -D ${DATA_DIR} -m immediate"

		retval=$?

		if [ $retval -eq 0 ] ; then
			ewarn "Server forced down."
			eend $retval
			return $retval
		fi

		eerror "Forced shutdown failed!!!"
		eerror "Something is wrong with your system."
		eerror "Please take care of it manually."
		eerror "Unable to stop server."
		eend $retval
		return $retval
	else
		ewarn "FORCE_QUIT disabled."
		eerror "Unable to shutdown server."
		eend 1
		return 1
	fi
}

reload() {
	ebegin "Reloading PostgreSQL configuration"
	su -l postgres \
		-c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
		/usr/lib/postgresql-8.2/bin/pg_ctl \
		reload -s -D ${DATA_DIR}"
	eend $?
}

status() {
	ebegin "Reloading PostgreSQL configuration"
	su -l postgres \
		-c "env PGPORT=\"${PGPORT}\" ${PG_EXTRA_ENV} \
		/usr/lib/postgresql-8.2/bin/pg_ctl \
		status -D ${DATA_DIR}"
	eend $?
}