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