Lines 1-14
Link Here
|
1 |
#!/sbin/openrc-run |
1 |
#!/sbin/openrc-run |
2 |
# Copyright 1999-2015 Gentoo Foundation |
2 |
# Copyright 1999-2017 Gentoo Foundation |
3 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# Distributed under the terms of the GNU General Public License v2 |
4 |
# $Id$ |
4 |
# $Id$ |
5 |
|
5 |
|
6 |
extra_started_commands="reload" |
6 |
extra_started_commands="reload promote" |
|
|
7 |
|
8 |
PG_CTL="/usr/@LIBDIR@/postgresql-@SLOT@/bin/pg_ctl" |
9 |
|
10 |
description="PostgreSQL @SLOT@ -- the world's most advanced open source database -- |
11 |
${RC_SERVICE} is a wrapper around pg_ctl with additional administrative checks |
12 |
and convenience" |
7 |
|
13 |
|
8 |
get_config() { |
14 |
get_config() { |
9 |
[ -f ${PGDATA%/}/postgresql.conf ] || return 1 |
15 |
[ -f "${PGDATA%/}/postgresql.conf" ] || return 1 |
10 |
|
16 |
|
11 |
eval echo $(sed -e 's:#.*::' ${PGDATA%/}/postgresql.conf \ |
17 |
eval echo $(sed -e 's:#.*::' "${PGDATA%/}/postgresql.conf" \ |
12 |
| awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }') |
18 |
| awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }') |
13 |
} |
19 |
} |
14 |
|
20 |
|
Lines 23-41
depend() {
Link Here
|
23 |
|
29 |
|
24 |
configured_port=$(get_config port) |
30 |
configured_port=$(get_config port) |
25 |
: ${configured_port:=${PGPORT}} |
31 |
: ${configured_port:=${PGPORT}} |
26 |
socket_paths=$(get_config unix_socket_directories) |
|
|
27 |
: ${socket_paths:=/run/postgresql} |
28 |
|
32 |
|
29 |
checkconfig() { |
33 |
checkconfig() { |
30 |
# Check that DATA_DIR has been set |
34 |
# Check that DATA_DIR has been set |
31 |
if [ -z ${DATA_DIR} ] ; then |
35 |
if [ -z "${DATA_DIR}" ] ; then |
32 |
eerror "DATA_DIR not set" |
36 |
eerror "DATA_DIR not set" |
33 |
eerror "HINT: Perhaps you need to update /etc/conf.d/postgresql-@SLOT@" |
37 |
eerror "HINT: Perhaps you need to update /etc/conf.d/postgresql-@SLOT@" |
34 |
return 1 |
38 |
return 1 |
35 |
fi |
39 |
fi |
36 |
|
40 |
|
37 |
# Check that DATA_DIR exists |
41 |
# Check that DATA_DIR exists |
38 |
if [ ! -d ${DATA_DIR} ] ; then |
42 |
if [ ! -d "${DATA_DIR}" ] ; then |
39 |
eerror "Directory not found: ${DATA_DIR}" |
43 |
eerror "Directory not found: ${DATA_DIR}" |
40 |
eerror "HINT: Ensure that DATA_DIR points to the right path." |
44 |
eerror "HINT: Ensure that DATA_DIR points to the right path." |
41 |
eerror "HINT: Or perhaps you need to create the database cluster:" |
45 |
eerror "HINT: Or perhaps you need to create the database cluster:" |
Lines 50-57
checkconfig() {
Link Here
|
50 |
local file |
54 |
local file |
51 |
for file in postgresql pg_hba pg_ident ; do |
55 |
for file in postgresql pg_hba pg_ident ; do |
52 |
file="${PGDATA%/}/${file}.conf" |
56 |
file="${PGDATA%/}/${file}.conf" |
53 |
if [ -f ${file} ] ; then |
57 |
if [ -f "${file}" ] ; then |
54 |
checkpath -f -m 0600 -o postgres:postgres ${file} |
58 |
checkpath -f -m 0600 -o postgres:postgres "${file}" |
55 |
else |
59 |
else |
56 |
eerror "${file} not found" |
60 |
eerror "${file} not found" |
57 |
eerror "HINT: mv ${DATA_DIR%/}/*.conf ${PGDATA}" |
61 |
eerror "HINT: mv ${DATA_DIR%/}/*.conf ${PGDATA}" |
Lines 61-110
checkconfig() {
Link Here
|
61 |
|
65 |
|
62 |
# Set the proper permission for the socket paths and create it if |
66 |
# Set the proper permission for the socket paths and create it if |
63 |
# it doesn't exist. |
67 |
# it doesn't exist. |
64 |
set -f; IFS=',' |
68 |
set -f; IFS=',' |
65 |
local s |
69 |
local s |
66 |
for s in ${socket_paths}; do |
70 |
for s in ${PG_SOCKET_DIRECTORIES}; do |
67 |
checkpath -d -m 1775 -o postgres:postgres ${s} |
71 |
checkpath -d -m 1775 -o root:postgres "${s}" |
68 |
if [ -e ${s%/}/.s.PGSQL.${configured_port} ] ; then |
72 |
if [ -e "${s%/}/.s.PGSQL.${configured_port}" ] ; then |
69 |
eerror "Socket conflict." |
73 |
eerror "Socket conflict." |
70 |
eerror "A server is already listening on:" |
74 |
eerror "A server is already listening on:" |
71 |
eerror " ${s%/}/.s.PGSQL.${configured_port}" |
75 |
eerror " ${s%/}/.s.PGSQL.${configured_port}" |
72 |
eerror "HINT: Change PGPORT to listen on a different socket." |
76 |
eerror "HINT: Change PGPORT to listen on a different socket." |
73 |
return 1 |
77 |
return 1 |
74 |
fi |
78 |
fi |
75 |
done |
79 |
done |
76 |
set +f; unset IFS |
80 |
set +f; unset IFS |
77 |
} |
81 |
} |
78 |
|
82 |
|
79 |
start() { |
83 |
start() { |
80 |
checkconfig || return 1 |
84 |
checkconfig || return 1 |
81 |
|
85 |
|
82 |
ebegin "Starting PostgreSQL" |
86 |
ebegin "Starting PostgreSQL @SLOT@" |
83 |
|
87 |
|
84 |
rm -f ${DATA_DIR%/}/postmaster.pid |
88 |
rm -f "${DATA_DIR%/}/postmaster.pid" |
85 |
|
89 |
|
86 |
local extraenv |
90 |
su - postgres -c \ |
87 |
local x |
91 |
"PGPORT=${configured_port} ${PG_EXTRA_ENV} ${PG_CTL} start \ |
88 |
for x in ${PG_EXTRA_ENV} ; do |
92 |
-s -w -t ${START_TIMEOUT} -l ${DATA_DIR%/}/postmaster.log \ |
89 |
extraenv="${extraenv} --env ${x}" |
93 |
-D ${PGDATA} \ |
90 |
done |
94 |
-o '--data-directory=${DATA_DIR} \ |
|
|
95 |
--unix-socket-directories=${PG_SOCKET_DIRECTORIES} \ |
96 |
${PGOPTS}'" |
91 |
|
97 |
|
92 |
start-stop-daemon --start \ |
|
|
93 |
--user postgres \ |
94 |
--env "PGPORT=${configured_port}" \ |
95 |
${extraenv} \ |
96 |
--pidfile ${DATA_DIR%/}/postmaster.pid \ |
97 |
--exec /usr/@LIBDIR@/postgresql-@SLOT@/bin/pg_ctl \ |
98 |
-- start -s -w -t ${START_TIMEOUT} -l ${DATA_DIR%/}/postmaster.log \ |
99 |
-D ${PGDATA} -o "--data-directory=${DATA_DIR} ${PGOPTS}" |
100 |
local retval=$? |
98 |
local retval=$? |
101 |
|
99 |
|
102 |
if [ $retval -ne 0 ] ; then |
100 |
if [ $retval -ne 0 ] ; then |
103 |
eerror "Check the log for a possible explanation of the above error. The log may be" |
101 |
eerror "Check the log for a possible explanation of the above error." |
|
|
102 |
eerror "The log may be located at:" |
104 |
eerror " ${DATA_DIR%/}/postmaster.log" |
103 |
eerror " ${DATA_DIR%/}/postmaster.log" |
105 |
eerror "Or wherever you configured PostgreSQL @SLOT@ log messages to be sent." |
104 |
eerror "Or wherever you configured PostgreSQL @SLOT@ to log." |
106 |
eend $retval |
|
|
107 |
return $retval |
108 |
fi |
105 |
fi |
109 |
|
106 |
|
110 |
eend $retval |
107 |
eend $retval |
Lines 112-143
start() {
Link Here
|
112 |
|
109 |
|
113 |
stop() { |
110 |
stop() { |
114 |
local seconds=$(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} )) |
111 |
local seconds=$(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} )) |
115 |
ebegin "Stopping PostgreSQL (this can take up to ${seconds} seconds)" |
112 |
ebegin "Stopping PostgreSQL @SLOT@ (this can take up to ${seconds} seconds)" |
116 |
|
113 |
|
117 |
local retval |
114 |
su - postgres -c \ |
118 |
local retries=SIGTERM/${NICE_TIMEOUT} |
115 |
"${PG_CTL} stop -t ${NICE_TIMEOUT} -s -D ${DATA_DIR} -m smart" |
|
|
116 |
local retval=$? |
119 |
|
117 |
|
120 |
if [ "${RUDE_QUIT}" != "NO" ] ; then |
118 |
if [ "${RUDE_QUIT}" != "NO" -a ${retval} -ne 0 ] ; then |
121 |
einfo "RUDE_QUIT enabled." |
119 |
einfo "Previous attempt failed. Trying RUDE_QUIT." |
122 |
retries="${retries}/SIGINT/${RUDE_TIMEOUT}" |
120 |
su - postgres -c \ |
|
|
121 |
"${PG_CTL} stop -t ${RUDE_TIMEOUT} -s -D ${DATA_DIR} -m fast" |
122 |
retval=$? |
123 |
fi |
123 |
fi |
124 |
if [ "${FORCE_QUIT}" = "YES" ] ; then |
124 |
|
125 |
einfo "FORCE_QUIT enabled." |
125 |
if [ "${FORCE_QUIT}" = "YES" -a ${retval} -ne 0 ] ; then |
|
|
126 |
einfo "Previous step failed. Trying FORCE_QUIT." |
126 |
ewarn "A recover-run might be executed on next startup." |
127 |
ewarn "A recover-run might be executed on next startup." |
127 |
retries="${retries}/SIGQUIT/${FORCE_TIMEOUT}" |
128 |
su - postgres -c \ |
|
|
129 |
"${PG_CTL} stop -t ${FORCE_TIMEOUT} -s -D ${DATA_DIR} -m immediate" |
130 |
retval=$? |
128 |
fi |
131 |
fi |
129 |
|
132 |
|
130 |
# Loops through nice, rude, and force quit in one go. |
133 |
eend ${retval} |
131 |
start-stop-daemon --stop \ |
134 |
} |
132 |
--exec /usr/@LIBDIR@/postgresql-@SLOT@/bin/postgres \ |
|
|
133 |
--retry ${retries} \ |
134 |
--pidfile ${DATA_DIR%/}/postmaster.pid |
135 |
|
135 |
|
136 |
eend |
136 |
status() { |
|
|
137 |
ebegin "Checking PostgreSQL @SLOT@ status" |
138 |
su - postgres -c "${PG_CTL} status -D ${DATA_DIR}" |
139 |
eend $? |
137 |
} |
140 |
} |
138 |
|
141 |
|
|
|
142 |
description_reload="Simply sends the postgres process a SIGHUP signal, causing |
143 |
it to reread its configuration files (postgresql.conf, pg_hba.conf, |
144 |
etc.). This allows changing of configuration-file options that do not |
145 |
require a complete restart to take effect." |
139 |
reload() { |
146 |
reload() { |
140 |
ebegin "Reloading PostgreSQL configuration" |
147 |
ebegin "Reloading PostgreSQL @SLOT@ configuration" |
141 |
kill -HUP $(head -n1 ${DATA_DIR%/}/postmaster.pid) |
148 |
su - postgres -c "${PG_CTL} reload -s -D ${DATA_DIR}" |
|
|
149 |
eend $? |
150 |
} |
151 |
|
152 |
description_promote="If the server is in standby, it is commanded to exit |
153 |
recovery and begin read-write operations." |
154 |
promote() { |
155 |
ebegin "Promoting PostgreSQL @SLOT@" |
156 |
su - postgres -c "${PG_CTL} promote -s -D ${DATA_DIR}" |
142 |
eend $? |
157 |
eend $? |
143 |
} |
158 |
} |