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