0aac8dd1aa22132ea55a4edf0f6e53b96373fe16
[gentoo.git] / dev-db / postgresql / postgresql-9.6.13-r1.ebuild
1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 EAPI=7
5
6 PYTHON_COMPAT=( python3_{6,7} )
7
8 PLOCALES="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr zh_CN
9                   zh_TW"
10
11 inherit flag-o-matic l10n linux-info multilib pam prefix python-single-r1 \
12                 systemd user
13
14 KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc ~x86 ~amd64-linux ~x86-linux ~ppc-macos ~x64-macos ~x86-macos ~sparc-solaris ~sparc64-solaris ~x64-solaris ~x86-solaris"
15
16 SLOT=$(ver_cut 1-2)
17
18 MY_PV=${PV/_/}
19 S="${WORKDIR}/${PN}-${MY_PV}"
20
21 SRC_URI="https://ftp.postgresql.org/pub/source/v${MY_PV}/postgresql-${MY_PV}.tar.bz2"
22
23 LICENSE="POSTGRESQL GPL-2"
24 DESCRIPTION="PostgreSQL RDBMS"
25 HOMEPAGE="https://www.postgresql.org/"
26
27 IUSE="debug doc kerberos kernel_linux ldap libressl nls pam perl
28           -pg_legacytimestamp python +readline selinux +server systemd ssl
29           static-libs tcl threads uuid xml zlib"
30
31 REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
32
33 CDEPEND="
34 >=app-eselect/eselect-postgresql-2.0
35 sys-apps/less
36 virtual/libintl
37 kerberos? ( virtual/krb5 )
38 ldap? ( net-nds/openldap )
39 pam? ( sys-libs/pam )
40 perl? ( >=dev-lang/perl-5.8:= )
41 python? ( ${PYTHON_DEPS} )
42 readline? ( sys-libs/readline:0= )
43 ssl? (
44         !libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
45         libressl? ( dev-libs/libressl:= )
46 )
47 server? ( systemd? ( sys-apps/systemd ) )
48 tcl? ( >=dev-lang/tcl-8:0= )
49 xml? ( dev-libs/libxml2 dev-libs/libxslt )
50 zlib? ( sys-libs/zlib )
51 "
52
53 # uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
54 # supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
55 # the libc includes UUID functions.
56 UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
57 BSD_LIBC=( elibc_{Free,Net,Open}BSD )
58
59 nest_usedep() {
60         local front back
61         while [[ ${#} -gt 1 ]]; do
62                 front+="${1}? ( "
63                 back+=" )"
64                 shift
65         done
66         echo "${front}${1}${back}"
67 }
68
69 IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
70 CDEPEND+="
71 uuid? (
72         ${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
73         $(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} dev-libs/ossp-uuid)
74 )"
75
76 DEPEND="${CDEPEND}
77 !!<sys-apps/sandbox-2.0
78 sys-devel/bison
79 sys-devel/flex
80 nls? ( sys-devel/gettext )
81 xml? ( virtual/pkgconfig )
82 "
83
84 RDEPEND="${CDEPEND}
85 !dev-db/postgresql-docs:${SLOT}
86 !dev-db/postgresql-base:${SLOT}
87 !dev-db/postgresql-server:${SLOT}
88 selinux? ( sec-policy/selinux-postgresql )
89 "
90
91 pkg_setup() {
92         use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
93
94         enewgroup postgres 70
95         enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
96
97         use python && python-single-r1_pkg_setup
98 }
99
100 src_prepare() {
101         # Work around PPC{,64} compilation bug where bool is already defined
102         sed '/#ifndef __cplusplus/a #undef bool' -i src/include/c.h || die
103
104         # Set proper run directory
105         sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
106                 -i src/include/pg_config_manual.h || die
107
108         # Rely on $PATH being in the proper order so that the correct
109         # install program is used for modules utilizing PGXS in both
110         # hardened and non-hardened environments. (Bug #528786)
111         sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
112
113         use server || eapply "${FILESDIR}/${PN}-${SLOT}.3-no-server.patch"
114
115         if use pam ; then
116                 sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
117                         -i src/backend/libpq/auth.c || \
118                         die 'PGSQL_PAM_SERVICE rename failed.'
119         fi
120
121         eapply_user
122 }
123
124 src_configure() {
125         case ${CHOST} in
126                 *-darwin*|*-solaris*)
127                         use nls && append-libs intl
128                         ;;
129         esac
130
131         export LDFLAGS_SL="${LDFLAGS}"
132         export LDFLAGS_EX="${LDFLAGS}"
133
134         local PO="${EPREFIX}"
135
136         local i uuid_config=""
137         if use uuid; then
138                 for i in ${UTIL_LINUX_LIBC[@]}; do
139                         use ${i} && uuid_config="--with-uuid=e2fs"
140                 done
141                 for i in ${BSD_LIBC[@]}; do
142                         use ${i} && uuid_config="--with-uuid=bsd"
143                 done
144                 [[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
145         fi
146
147         econf \
148                 --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
149                 --datadir="${PO}/usr/share/postgresql-${SLOT}" \
150                 --docdir="${PO}/usr/share/doc/${PF}" \
151                 --includedir="${PO}/usr/include/postgresql-${SLOT}" \
152                 --mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
153                 --sysconfdir="${PO}/etc/postgresql-${SLOT}" \
154                 --with-system-tzdata="${PO}/usr/share/zoneinfo" \
155                 $(use_enable !alpha spinlocks) \
156                 $(use_enable !pg_legacytimestamp integer-datetimes) \
157                 $(use_enable debug) \
158                 $(use_enable threads thread-safety) \
159                 $(use_with kerberos gssapi) \
160                 $(use_with ldap) \
161                 $(use_with pam) \
162                 $(use_with perl) \
163                 $(use_with python) \
164                 $(use_with readline) \
165                 $(use_with ssl openssl) \
166                 $(usex server "$(use_with systemd)" '--without-systemd') \
167                 $(use_with tcl) \
168                 ${uuid_config} \
169                 $(use_with xml libxml) \
170                 $(use_with xml libxslt) \
171                 $(use_with zlib) \
172                 $(use_enable nls nls "'$(l10n_get_locales)'")
173 }
174
175 src_compile() {
176         emake
177         emake -C contrib
178 }
179
180 src_install() {
181         emake DESTDIR="${D}" install
182         emake DESTDIR="${D}" install -C contrib
183
184         dodoc README HISTORY doc/{TODO,bug.template}
185
186         # man pages are already built, but if we have the target make them,
187         # they'll be generated from source before being installed so we
188         # manually install man pages.
189         # We use ${SLOT} instead of doman for postgresql.eselect
190         insinto /usr/share/postgresql-${SLOT}/man/
191         doins -r doc/src/sgml/man{1,3,7}
192         if ! use server; then
193                 # Remove man pages for non-existent binaries
194                 for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
195                         rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
196                 done
197         fi
198         docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
199
200         # Create slot specific man pages
201         local bn f mansec slotted_name
202         for mansec in 1 3 7 ; do
203                 local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
204
205                 mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
206                 pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
207
208                 for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
209                         bn=$(basename "${f}")
210                         slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
211                         case ${bn} in
212                                 TABLE.7|WITH.7)
213                                         echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
214                                         ;;
215                                 *)
216                                         echo ".so ${rel_manpath}/${bn}" > ${slotted_name}
217                                         ;;
218                         esac
219                 done
220
221                 popd > /dev/null
222         done
223
224         insinto /etc/postgresql-${SLOT}
225         newins src/bin/psql/psqlrc.sample psqlrc
226
227         # Don't delete libpg{port,common}.a (Bug #571046). They're always
228         # needed by extensions utilizing PGXS.
229         use static-libs || \
230                 find "${ED}" -name '*.a' ! -name libpgport.a ! -name libpgcommon.a \
231                          -delete
232
233         local f bn
234         for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
235                                         -mindepth 1 -maxdepth 1)
236         do
237                 bn=$(basename "${f}")
238                 # Temporarily tack on tmp to workaround a file collision
239                 # issue. This is only necessary for 9.7 and earlier. 10 never
240                 # had this issue.
241                 dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
242                           "/usr/bin/${bn}${SLOT/.}tmp"
243         done
244
245         if use doc ; then
246                 docinto html
247                 dodoc doc/src/sgml/html/*
248
249                 docinto sgml
250                 dodoc doc/src/sgml/*.{sgml,dsl}
251         fi
252
253         if use server; then
254                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
255                         "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
256
257                 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
258                         "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
259
260                 if use systemd; then
261                         sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
262                                 "${FILESDIR}/${PN}.service-9.6-r1" | \
263                                 systemd_newunit - ${PN}-${SLOT}.service
264                         systemd_newtmpfilesd "${FILESDIR}"/${PN}.tmpfiles ${PN}-${SLOT}.conf
265                 fi
266
267                 newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
268
269                 use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
270
271                 if use prefix ; then
272                         keepdir /run/postgresql
273                         fperms 1775 /run/postgresql
274                 fi
275         fi
276 }
277
278 pkg_preinst() {
279         # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
280         # /usr/bin/psql96). They may have been created by the
281         # postgresql.eselect module, but they're handled within this ebuild
282         # now. It's alright if we momentarily delete /usr/bin/psql as it
283         # will be recreated by the eselect module in pkg_ppostinst(). This
284         # is only necessary for 9.7 and earlier. 10 and later were never
285         # handled in this manner.
286         local canonicalise
287         if type -p realpath > /dev/null; then
288                 canonicalise=realpath
289         elif type -p readlink > /dev/null; then
290                 canonicalise='readlink -f'
291         else
292                 # can't die, subshell
293                 die "No readlink nor realpath found, cannot canonicalise"
294         fi
295
296         local l
297         # First remove any symlinks in /usr/bin that may have been created
298         # by the old eselect
299         for l in $(find "${ROOT}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
300                 if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
301                         rm "${l}" || ewarn "Couldn't remove ${l}"
302                 fi
303         done
304
305         # Then move the symlinks created by the ebuild to their proper place.
306         for l in "${ED}"/usr/bin/*tmp ; do
307                 mv "${l}" "${l%tmp}" \
308                         || ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
309         done
310 }
311
312 pkg_postinst() {
313         use server && use systemd && systemd_tmpfiles_create ${PN}-${SLOT}.conf
314         postgresql-config update
315
316         elog "If you need a global psqlrc-file, you can place it in:"
317         elog "    ${EROOT}/etc/postgresql-${SLOT}/"
318
319         if use server ; then
320                 elog
321                 elog "Gentoo specific documentation:"
322                 elog "https://wiki.gentoo.org/wiki/PostgreSQL"
323                 elog
324                 elog "Official documentation:"
325                 elog "https://www.postgresql.org/docs/${SLOT}/static/index.html"
326                 elog
327                 elog "The default location of the Unix-domain socket is:"
328                 elog "    ${EROOT}/run/postgresql/"
329                 elog
330                 elog "Before initializing the database, you may want to edit PG_INITDB_OPTS"
331                 elog "so that it contains your preferred locale in:"
332                 elog "    ${EROOT}/etc/conf.d/postgresql-${SLOT}"
333                 elog
334                 elog "Then, execute the following command to setup the initial database"
335                 elog "environment:"
336                 elog "    emerge --config =${CATEGORY}/${PF}"
337         fi
338 }
339
340 pkg_prerm() {
341         if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
342                 ewarn "Have you dumped and/or migrated the ${SLOT} database cluster?"
343                 ewarn "\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL"
344
345                 ebegin "Resuming removal in 10 seconds (Control-C to cancel)"
346                 sleep 10
347                 eend 0
348         fi
349 }
350
351 pkg_postrm() {
352         postgresql-config update
353 }
354
355 pkg_config() {
356         use server || die "USE flag 'server' not enabled. Nothing to configure."
357
358         [[ -f "${EROOT}/etc/conf.d/postgresql-${SLOT}" ]] \
359                 && source "${EROOT}/etc/conf.d/postgresql-${SLOT}"
360         [[ -z "${PGDATA}" ]] && PGDATA="${EROOT}/etc/postgresql-${SLOT}/"
361         [[ -z "${DATA_DIR}" ]] \
362                 && DATA_DIR="${EROOT}/var/lib/postgresql/${SLOT}/data"
363
364         # environment.bz2 may not contain the same locale as the current system
365         # locale. Unset and source from the current system locale.
366         if [ -f "${EROOT}/etc/env.d/02locale" ]; then
367                 unset LANG
368                 unset LC_CTYPE
369                 unset LC_NUMERIC
370                 unset LC_TIME
371                 unset LC_COLLATE
372                 unset LC_MONETARY
373                 unset LC_MESSAGES
374                 unset LC_ALL
375                 source "${EROOT}/etc/env.d/02locale"
376                 [ -n "${LANG}" ] && export LANG
377                 [ -n "${LC_CTYPE}" ] && export LC_CTYPE
378                 [ -n "${LC_NUMERIC}" ] && export LC_NUMERIC
379                 [ -n "${LC_TIME}" ] && export LC_TIME
380                 [ -n "${LC_COLLATE}" ] && export LC_COLLATE
381                 [ -n "${LC_MONETARY}" ] && export LC_MONETARY
382                 [ -n "${LC_MESSAGES}" ] && export LC_MESSAGES
383                 [ -n "${LC_ALL}" ] && export LC_ALL
384         fi
385
386         einfo "You can modify the paths and options passed to initdb by editing:"
387         einfo "    ${EROOT}/etc/conf.d/postgresql-${SLOT}"
388         einfo
389         einfo "Information on options that can be passed to initdb are found at:"
390         einfo "    https://www.postgresql.org/docs/${SLOT}/static/creating-cluster.html"
391         einfo "    https://www.postgresql.org/docs/${SLOT}/static/app-initdb.html"
392         einfo
393         einfo "PG_INITDB_OPTS is currently set to:"
394         if [[ -z "${PG_INITDB_OPTS}" ]] ; then
395                 einfo "    (none)"
396         else
397                 einfo "    ${PG_INITDB_OPTS}"
398         fi
399         einfo
400         einfo "Configuration files will be installed to:"
401         einfo "    ${PGDATA}"
402         einfo
403         einfo "The database cluster will be created in:"
404         einfo "    ${DATA_DIR}"
405         einfo
406
407         ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
408         sleep 5
409         eend 0
410
411         if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
412                 eerror "The given directory, '${DATA_DIR}', is not empty."
413                 eerror "Modify DATA_DIR to point to an empty directory."
414                 die "${DATA_DIR} is not empty."
415         fi
416
417         einfo "Creating the data directory ..."
418         if [[ ${EUID} == 0 ]] ; then
419                 mkdir -p "${DATA_DIR}"
420                 chown -Rf postgres:postgres "${DATA_DIR}"
421                 chmod 0700 "${DATA_DIR}"
422         fi
423
424         einfo "Initializing the database ..."
425
426         if [[ ${EUID} == 0 ]] ; then
427                 su postgres -c "${EROOT}/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -D \"${DATA_DIR}\" ${PG_INITDB_OPTS}"
428         else
429                 "${EROOT}"/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -U postgres -D "${DATA_DIR}" ${PG_INITDB_OPTS}
430         fi
431
432         if [[ "${DATA_DIR%/}" != "${PGDATA%/}" ]] ; then
433                 mv "${DATA_DIR%/}"/{pg_{hba,ident},postgresql}.conf "${PGDATA}"
434                 ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf "${DATA_DIR%/}"
435         fi
436
437         # unix_socket_directory has no effect in postgresql.conf as it's
438         # overridden in the initscript
439         sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
440
441         cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
442                 # This is here because of https://bugs.gentoo.org/show_bug.cgi?id=518522
443                 # On the off-chance that you might need to work with UTF-8 encoded
444                 # characters in PL/Perl
445                 plperl.on_init = 'use utf8; use re; package utf8; require "utf8_heavy.pl";'
446         EOF
447
448         einfo "The autovacuum function, which was in contrib, has been moved to the main"
449         einfo "PostgreSQL functions starting with 8.1, and starting with 8.4 is now enabled"
450         einfo "by default. You can disable it in the cluster's:"
451         einfo "    ${PGDATA%/}/postgresql.conf"
452         einfo
453         if ! use systemd; then
454                 einfo "The PostgreSQL server, by default, will log events to:"
455                 einfo "    ${DATA_DIR%/}/postmaster.log"
456                 einfo
457         fi
458         if use prefix ; then
459                 einfo "The location of the configuration files have moved to:"
460                 einfo "    ${PGDATA}"
461                 einfo "To start the server:"
462                 einfo "    pg_ctl start -D ${DATA_DIR} -o '-D ${PGDATA} --data-directory=${DATA_DIR}'"
463                 einfo "To stop:"
464                 einfo "    pg_ctl stop -D ${DATA_DIR}"
465                 einfo
466                 einfo "Or move the configuration files back:"
467                 einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
468         elif use systemd; then
469                 einfo "You should use the 'postgresql-${SLOT}.service' unit to run PostgreSQL"
470                 einfo "instead of 'pg_ctl'."
471         else
472                 einfo "You should use the '${EROOT}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
473                 einfo "instead of 'pg_ctl'."
474         fi
475 }
476
477 src_test() {
478         if use server && [[ ${UID} -ne 0 ]] ; then
479                 emake check
480
481                 einfo "If you think other tests besides the regression tests are necessary, please"
482                 einfo "submit a bug including a patch for this ebuild to enable them."
483         else
484                 use server || \
485                         ewarn 'Tests cannot be run without the "server" use flag enabled.'
486                 [[ ${UID} -eq 0 ]] || \
487                         ewarn 'Tests cannot be run as root. Enable "userpriv" in FEATURES.'
488
489                 ewarn 'Skipping.'
490         fi
491 }