dev-db/postgresql: amd64 stable wrt bug #574456
[gentoo.git] / dev-db / postgresql / postgresql-9.1.20.ebuild
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 EAPI="5"
6
7 # Testing within Portage's environment is broken, and the patch no
8 # longer applies cleanly.
9 RESTRICT="test"
10
11 PYTHON_COMPAT=( python{2_7,3_4} )
12
13 inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
14                 systemd user versionator
15
16 KEYWORDS="~alpha amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
17
18 SLOT="$(get_version_component_range 1-2)"
19
20 SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
21
22 LICENSE="POSTGRESQL GPL-2"
23 DESCRIPTION="PostgreSQL RDBMS"
24 HOMEPAGE="http://www.postgresql.org/"
25
26 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
27                  zh_CN zh_TW"
28 IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp python
29           +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
30
31 for lingua in ${LINGUAS}; do
32         IUSE+=" linguas_${lingua}"
33 done
34
35 wanted_languages() {
36         local enable_langs
37
38         for lingua in ${LINGUAS} ; do
39                 use linguas_${lingua} && enable_langs+="${lingua} "
40         done
41
42         echo -n ${enable_langs}
43 }
44
45 CDEPEND="
46 >=app-eselect/eselect-postgresql-1.2.0
47 sys-apps/less
48 virtual/libintl
49 kerberos? ( virtual/krb5 )
50 ldap? ( net-nds/openldap )
51 pam? ( virtual/pam )
52 perl? ( >=dev-lang/perl-5.8 )
53 python? ( ${PYTHON_DEPS} )
54 readline? ( sys-libs/readline:0= )
55 ssl? (
56         !libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
57         libressl? ( dev-libs/libressl:= )
58 )
59 tcl? ( >=dev-lang/tcl-8:0= )
60 uuid? ( dev-libs/ossp-uuid )
61 xml? ( dev-libs/libxml2 dev-libs/libxslt )
62 zlib? ( sys-libs/zlib )
63 "
64
65 DEPEND="${CDEPEND}
66 !!<sys-apps/sandbox-2.0
67 sys-devel/bison
68 sys-devel/flex
69 nls? ( sys-devel/gettext )
70 xml? ( virtual/pkgconfig )
71 "
72
73 RDEPEND="${CDEPEND}
74 !dev-db/postgresql-docs:${SLOT}
75 !dev-db/postgresql-base:${SLOT}
76 !dev-db/postgresql-server:${SLOT}
77 selinux? ( sec-policy/selinux-postgresql )
78 "
79
80 pkg_setup() {
81         use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
82
83         enewgroup postgres 70
84         enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
85
86         use python && python-single-r1_pkg_setup
87 }
88
89 src_prepare() {
90         # Work around PPC{,64} compilation bug where bool is already defined
91         sed '/#ifndef __cplusplus/a #undef bool' -i src/include/c.h || die
92
93         # Set proper run directory
94         sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
95                 -i src/include/pg_config_manual.h || die
96
97         epatch "${FILESDIR}/pg_ctl-exit-status.patch"
98
99         use server || epatch "${FILESDIR}/${PN}-${SLOT}-no-server.patch"
100
101         # Fix bug 486556 where the server would crash at start up because of
102         # an infinite loop caused by a self-referencing symlink.
103         epatch "${FILESDIR}/postgresql-9.1-tz-dir-overflow.patch"
104
105         if use pam ; then
106                 sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
107                         -i src/backend/libpq/auth.c || \
108                         die 'PGSQL_PAM_SERVICE rename failed.'
109         fi
110
111         epatch_user
112 }
113
114 src_configure() {
115         case ${CHOST} in
116                 *-darwin*|*-solaris*)
117                         use nls && append-libs intl
118                         ;;
119         esac
120
121         export LDFLAGS_SL="${LDFLAGS}"
122         export LDFLAGS_EX="${LDFLAGS}"
123
124         local PO="${EPREFIX%/}"
125
126         econf \
127                 --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
128                 --datadir="${PO}/usr/share/postgresql-${SLOT}" \
129                 --docdir="${PO}/usr/share/doc/${PF}" \
130                 --includedir="${PO}/usr/include/postgresql-${SLOT}" \
131                 --mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
132                 --sysconfdir="${PO}/etc/postgresql-${SLOT}" \
133                 --with-system-tzdata="${PO}/usr/share/zoneinfo" \
134                 $(use_enable !pg_legacytimestamp integer-datetimes) \
135                 $(use_enable threads thread-safety) \
136                 $(use_with kerberos gssapi) \
137                 $(use_with kerberos krb5) \
138                 $(use_with ldap) \
139                 $(use_with pam) \
140                 $(use_with perl) \
141                 $(use_with python) \
142                 $(use_with readline) \
143                 $(use_with ssl openssl) \
144                 $(use_with tcl) \
145                 $(use_with uuid ossp-uuid) \
146                 $(use_with xml libxml) \
147                 $(use_with xml libxslt) \
148                 $(use_with zlib) \
149                 "$(use_enable nls nls "$(wanted_languages)")"
150 }
151
152 src_compile() {
153         emake
154         emake -C contrib
155 }
156
157 src_install() {
158         emake DESTDIR="${D}" install
159         emake DESTDIR="${D}" install -C contrib
160
161         dodoc README HISTORY doc/{TODO,bug.template}
162
163         # man pages are already built, but if we have the target make them,
164         # they'll be generated from source before being installed so we
165         # manually install man pages.
166         # We use ${SLOT} instead of doman for postgresql.eselect
167         insinto /usr/share/postgresql-${SLOT}/man/
168         doins -r doc/src/sgml/man{1,3,7}
169         if ! use server; then
170                 # Remove man pages for non-existent binaries
171                 for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
172                         rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
173                 done
174         fi
175         docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
176
177         insinto /etc/postgresql-${SLOT}
178         newins src/bin/psql/psqlrc.sample psqlrc
179
180         dodir /etc/eselect/postgresql/slots/${SLOT}
181         echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
182                 "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
183
184         use static-libs || find "${ED}" -name '*.a' -delete
185
186         if use doc ; then
187                 docinto html
188                 dodoc doc/src/sgml/html/*
189
190                 docinto sgml
191                 dodoc doc/src/sgml/*.{sgml,dsl}
192         fi
193
194         if use server; then
195                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
196                         "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
197
198                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
199                         "${FILESDIR}/${PN}.init-pre_9.2" | newinitd - ${PN}-${SLOT}
200
201                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
202                         "${FILESDIR}/${PN}.service" | \
203                         systemd_newunit - ${PN}-${SLOT}.service
204
205                 newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
206
207                 use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
208
209                 if use prefix ; then
210                         keepdir /run/postgresql
211                         fperms 0775 /run/postgresql
212                 fi
213         fi
214 }
215
216 pkg_postinst() {
217         postgresql-config update
218
219         elog "If you need a global psqlrc-file, you can place it in:"
220         elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
221
222         if [[ -z ${REPLACING_VERSIONS} ]] ; then
223                 elog
224                 elog "It looks like this is your first time installing PostgreSQL. Run the"
225                 elog "following command in all active shells to pick up changes to the default"
226                 elog "environemnt:"
227                 elog "    source /etc/profile"
228         fi
229
230         if use server ; then
231                 elog
232                 elog "Gentoo specific documentation:"
233                 elog "https://wiki.gentoo.org/wiki/PostgreSQL"
234                 elog
235                 elog "Official documentation:"
236                 elog "http://www.postgresql.org/docs/${SLOT}/static/index.html"
237                 elog
238                 elog "The default location of the Unix-domain socket is:"
239                 elog "    ${EROOT%/}/run/postgresql/"
240                 elog
241                 elog "Before initializing the database, you may want to edit PG_INITDB_OPTS"
242                 elog "so that it contains your preferred locale in:"
243                 elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
244                 elog
245                 elog "Then, execute the following command to setup the initial database"
246                 elog "environment:"
247                 elog "    emerge --config =${CATEGORY}/${PF}"
248         fi
249 }
250
251 pkg_prerm() {
252         if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
253                 ewarn "Have you dumped and/or migrated the ${SLOT} database cluster?"
254                 ewarn "\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL"
255
256                 ebegin "Resuming removal in 10 seconds (Control-C to cancel)"
257                 sleep 10
258                 eend 0
259         fi
260 }
261
262 pkg_postrm() {
263         postgresql-config update
264 }
265
266 pkg_config() {
267         use server || die "USE flag 'server' not enabled. Nothing to configure."
268
269         [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
270                 && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
271         [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
272         [[ -z "${DATA_DIR}" ]] \
273                 && DATA_DIR="${EROOT%/}/var/lib/postgresql/${SLOT}/data"
274
275         # environment.bz2 may not contain the same locale as the current system
276         # locale. Unset and source from the current system locale.
277         if [ -f "${EROOT%/}/etc/env.d/02locale" ]; then
278                 unset LANG
279                 unset LC_CTYPE
280                 unset LC_NUMERIC
281                 unset LC_TIME
282                 unset LC_COLLATE
283                 unset LC_MONETARY
284                 unset LC_MESSAGES
285                 unset LC_ALL
286                 source "${EROOT%/}/etc/env.d/02locale"
287                 [ -n "${LANG}" ] && export LANG
288                 [ -n "${LC_CTYPE}" ] && export LC_CTYPE
289                 [ -n "${LC_NUMERIC}" ] && export LC_NUMERIC
290                 [ -n "${LC_TIME}" ] && export LC_TIME
291                 [ -n "${LC_COLLATE}" ] && export LC_COLLATE
292                 [ -n "${LC_MONETARY}" ] && export LC_MONETARY
293                 [ -n "${LC_MESSAGES}" ] && export LC_MESSAGES
294                 [ -n "${LC_ALL}" ] && export LC_ALL
295         fi
296
297         einfo "You can modify the paths and options passed to initdb by editing:"
298         einfo "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
299         einfo
300         einfo "Information on options that can be passed to initdb are found at:"
301         einfo "    http://www.postgresql.org/docs/${SLOT}/static/creating-cluster.html"
302         einfo "    http://www.postgresql.org/docs/${SLOT}/static/app-initdb.html"
303         einfo
304         einfo "PG_INITDB_OPTS is currently set to:"
305         if [[ -z "${PG_INITDB_OPTS}" ]] ; then
306                 einfo "    (none)"
307         else
308                 einfo "    ${PG_INITDB_OPTS}"
309         fi
310         einfo
311         einfo "Configuration files will be installed to:"
312         einfo "    ${PGDATA}"
313         einfo
314         einfo "The database cluster will be created in:"
315         einfo "    ${DATA_DIR}"
316         einfo
317         while [ "$correct" != "true" ] ; do
318                 einfo "Are you ready to continue? (y/n)"
319                 read answer
320                 if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
321                         correct="true"
322                 elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
323                         die "Aborting initialization."
324                 else
325                         echo "Answer not recognized"
326                 fi
327         done
328
329         if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
330                 eerror "The given directory, '${DATA_DIR}', is not empty."
331                 eerror "Modify DATA_DIR to point to an empty directory."
332                 die "${DATA_DIR} is not empty."
333         fi
334
335         einfo "Creating the data directory ..."
336         if [[ ${EUID} == 0 ]] ; then
337                 mkdir -p "${DATA_DIR}"
338                 chown -Rf postgres:postgres "${DATA_DIR}"
339                 chmod 0700 "${DATA_DIR}"
340         fi
341
342         einfo "Initializing the database ..."
343
344         if [[ ${EUID} == 0 ]] ; then
345                 su postgres -c "${EROOT%/}/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -D \"${DATA_DIR}\" ${PG_INITDB_OPTS}"
346         else
347                 "${EROOT%/}"/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -U postgres -D "${DATA_DIR}" ${PG_INITDB_OPTS}
348         fi
349
350         if [[ "${DATA_DIR%/}" != "${PGDATA%/}" ]] ; then
351                 mv "${DATA_DIR%/}"/{pg_{hba,ident},postgresql}.conf "${PGDATA}"
352                 ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf "${DATA_DIR%/}"
353         fi
354
355         cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
356                 # This is here because of https://bugs.gentoo.org/show_bug.cgi?id=518522
357                 # On the off-chance that you might need to work with UTF-8 encoded
358                 # characters in PL/Perl
359                 plperl.on_init = 'use utf8; use re; package utf8; require "utf8_heavy.pl";'
360         EOF
361
362         einfo "The autovacuum function, which was in contrib, has been moved to the main"
363         einfo "PostgreSQL functions starting with 8.1, and starting with 8.4 is now enabled"
364         einfo "by default. You can disable it in the cluster's:"
365         einfo "    ${PGDATA%/}/postgresql.conf"
366         einfo
367         einfo "The PostgreSQL server, by default, will log events to:"
368         einfo "    ${DATA_DIR%/}/postmaster.log"
369         einfo
370         if use prefix ; then
371                 einfo "The location of the configuration files have moved to:"
372                 einfo "    ${PGDATA}"
373                 einfo "To start the server:"
374                 einfo "    pg_ctl start -D ${DATA_DIR} -o '-D ${PGDATA} --data-directory=${DATA_DIR}'"
375                 einfo "To stop:"
376                 einfo "    pg_ctl stop -D ${DATA_DIR}"
377                 einfo
378                 einfo "Or move the configuration files back:"
379                 einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
380         else
381                 einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
382                 einfo "instead of 'pg_ctl'."
383         fi
384 }