1 # Copyright 1999-2020 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
6 PYTHON_COMPAT=( python2_7 python3_6 )
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
11 inherit flag-o-matic l10n linux-info multilib pam prefix python-single-r1 \
12 systemd user versionator
14 KEYWORDS="alpha amd64 arm ~arm64 hppa ia64 ~mips ppc ppc64 ~s390 ~sh sparc x86 ~ppc-macos ~x86-solaris"
16 SLOT="$(get_version_component_range 1-2)"
18 SRC_URI="https://ftp.postgresql.org/pub/source/v${PV}/postgresql-${PV}.tar.bz2"
20 LICENSE="POSTGRESQL GPL-2"
21 DESCRIPTION="PostgreSQL RDBMS"
22 HOMEPAGE="https://www.postgresql.org/"
24 IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp
25 python +readline selinux +server systemd ssl static-libs tcl threads uuid
28 REQUIRED_USE="python? ( ${PYTHON_REQUIRED_USE} )"
31 >=app-eselect/eselect-postgresql-2.0
34 kerberos? ( virtual/krb5 )
35 ldap? ( net-nds/openldap )
37 perl? ( >=dev-lang/perl-5.8:= )
38 python? ( ${PYTHON_DEPS} )
39 readline? ( sys-libs/readline:0= )
41 !libressl? ( >=dev-libs/openssl-0.9.6-r1:0= )
42 libressl? ( dev-libs/libressl:= )
44 tcl? ( >=dev-lang/tcl-8:0= )
45 xml? ( dev-libs/libxml2 dev-libs/libxslt )
46 zlib? ( sys-libs/zlib )
49 # uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
50 # supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
51 # the libc includes UUID functions.
52 UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
53 BSD_LIBC=( elibc_{Free,Net,Open}BSD )
57 while [[ ${#} -gt 1 ]]; do
62 echo "${front}${1}${back}"
65 IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
68 ${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
69 $(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} dev-libs/ossp-uuid)
73 !!<sys-apps/sandbox-2.0
76 nls? ( sys-devel/gettext )
77 xml? ( virtual/pkgconfig )
81 !dev-db/postgresql-docs:${SLOT}
82 !dev-db/postgresql-base:${SLOT}
83 !dev-db/postgresql-server:${SLOT}
84 selinux? ( sec-policy/selinux-postgresql )
88 use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
91 enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
93 use python && python-single-r1_pkg_setup
97 # Work around PPC{,64} compilation bug where bool is already defined
98 sed '/#ifndef __cplusplus/a #undef bool' -i src/include/c.h || die
100 # Set proper run directory
101 sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
102 -i src/include/pg_config_manual.h || die
104 # Rely on $PATH being in the proper order so that the correct
105 # install program is used for modules utilizing PGXS in both
106 # hardened and non-hardened environments. (Bug #528786)
107 sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
109 use server || eapply "${FILESDIR}/${PN}-9.5.5-no-server.patch"
112 sed -e "s/\(#define PGSQL_PAM_SERVICE \"postgresql\)/\1-${SLOT}/" \
113 -i src/backend/libpq/auth.c || \
114 die 'PGSQL_PAM_SERVICE rename failed.'
122 *-darwin*|*-solaris*)
123 use nls && append-libs intl
127 export LDFLAGS_SL="${LDFLAGS}"
128 export LDFLAGS_EX="${LDFLAGS}"
130 local PO="${EPREFIX%/}"
132 local i uuid_config=""
134 for i in ${UTIL_LINUX_LIBC[@]}; do
135 use ${i} && uuid_config="--with-uuid=e2fs"
137 for i in ${BSD_LIBC[@]}; do
138 use ${i} && uuid_config="--with-uuid=bsd"
140 [[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
144 --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
145 --datadir="${PO}/usr/share/postgresql-${SLOT}" \
146 --docdir="${PO}/usr/share/doc/${PF}" \
147 --includedir="${PO}/usr/include/postgresql-${SLOT}" \
148 --mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
149 --sysconfdir="${PO}/etc/postgresql-${SLOT}" \
150 --with-system-tzdata="${PO}/usr/share/zoneinfo" \
151 $(use_enable !alpha spinlocks) \
152 $(use_enable !pg_legacytimestamp integer-datetimes) \
153 $(use_enable threads thread-safety) \
154 $(use_with kerberos gssapi) \
159 $(use_with readline) \
160 $(use_with ssl openssl) \
163 $(use_with xml libxml) \
164 $(use_with xml libxslt) \
166 $(use_enable nls nls "'$(l10n_get_locales)'")
175 emake DESTDIR="${D}" install
176 emake DESTDIR="${D}" install -C contrib
178 dodoc README HISTORY doc/{TODO,bug.template}
180 # man pages are already built, but if we have the target make them,
181 # they'll be generated from source before being installed so we
182 # manually install man pages.
183 # We use ${SLOT} instead of doman for postgresql.eselect
184 insinto /usr/share/postgresql-${SLOT}/man/
185 doins -r doc/src/sgml/man{1,3,7}
186 if ! use server; then
187 # Remove man pages for non-existent binaries
188 for m in {initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
189 rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
192 docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
194 # Create slot specific man pages
195 local bn f mansec slotted_name
196 for mansec in 1 3 7 ; do
197 local rel_manpath="../../postgresql-${SLOT}/man/man${mansec}"
199 mkdir -p "${ED}"/usr/share/man/man${mansec} || die "making man dir"
200 pushd "${ED}"/usr/share/man/man${mansec} > /dev/null || die "pushd failed"
202 for f in "${ED}/usr/share/postgresql-${SLOT}/man/man${mansec}"/* ; do
203 bn=$(basename "${f}")
204 slotted_name=${bn%.${mansec}}${SLOT/.}.${mansec}
207 echo ".so ${rel_manpath}/SELECT.7" > ${slotted_name}
210 echo ".so ${rel_manpath}/${bn}" > ${slotted_name}
218 insinto /etc/postgresql-${SLOT}
219 newins src/bin/psql/psqlrc.sample psqlrc
221 use static-libs || find "${ED}" -name '*.a' -delete
224 for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
225 -mindepth 1 -maxdepth 1)
227 bn=$(basename "${f}")
228 # Temporarily tack on tmp to workaround a file collision
229 # issue. This is only necessary for 9.7 and earlier. 10 never
231 dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
232 "/usr/bin/${bn}${SLOT/.}tmp"
237 dodoc doc/src/sgml/html/*
240 dodoc doc/src/sgml/*.{sgml,dsl}
244 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
245 "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
247 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
248 "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
251 sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
252 "${FILESDIR}/${PN}.service-9.2" | \
253 systemd_newunit - ${PN}-${SLOT}.service
254 systemd_newtmpfilesd "${FILESDIR}"/${PN}.tmpfiles ${PN}-${SLOT}.conf
257 newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
259 use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
262 keepdir /run/postgresql
263 fperms 1775 /run/postgresql
269 # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
270 # /usr/bin/psql96). They may have been created by the
271 # postgresql.eselect module, but they're handled within this ebuild
272 # now. It's alright if we momentarily delete /usr/bin/psql as it
273 # will be recreated by the eselect module in pkg_ppostinst(). This
274 # is only necessary for 9.7 and earlier. 10 and later were never
275 # handled in this manner.
277 if type -p realpath > /dev/null; then
278 canonicalise=realpath
279 elif type -p readlink > /dev/null; then
280 canonicalise='readlink -f'
282 # can't die, subshell
283 die "No readlink nor realpath found, cannot canonicalise"
287 # First remove any symlinks in /usr/bin that may have been created
289 for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; do
290 if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; then
291 rm "${l}" || ewarn "Couldn't remove ${l}"
295 # Then move the symlinks created by the ebuild to their proper place.
296 for l in "${ED}"/usr/bin/*tmp ; do
297 mv "${l}" "${l%tmp}" \
298 || ewarn "Couldn't rename $(basename ${l}) to $(basename ${l%tmp})"
303 use server && use systemd && systemd_tmpfiles_create ${PN}-${SLOT}.conf
304 postgresql-config update
306 if use alpha && use server ; then
307 ewarn "PostgreSQL 9.5+ no longer has native spinlock support on Alpha platforms."
308 ewarn "As a result, performance will be extremely degraded."
311 elog "If you need a global psqlrc-file, you can place it in:"
312 elog " ${EROOT%/}/etc/postgresql-${SLOT}/"
316 elog "Gentoo specific documentation:"
317 elog "https://wiki.gentoo.org/wiki/PostgreSQL"
319 elog "Official documentation:"
320 elog "https://www.postgresql.org/docs/${SLOT}/static/index.html"
322 elog "The default location of the Unix-domain socket is:"
323 elog " ${EROOT%/}/run/postgresql/"
325 elog "Before initializing the database, you may want to edit PG_INITDB_OPTS"
326 elog "so that it contains your preferred locale in:"
327 elog " ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
329 elog "Then, execute the following command to setup the initial database"
331 elog " emerge --config =${CATEGORY}/${PF}"
336 if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
337 ewarn "Have you dumped and/or migrated the ${SLOT} database cluster?"
338 ewarn "\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL"
340 ebegin "Resuming removal in 10 seconds (Control-C to cancel)"
347 postgresql-config update
351 use server || die "USE flag 'server' not enabled. Nothing to configure."
353 [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
354 && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
355 [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
356 [[ -z "${DATA_DIR}" ]] \
357 && DATA_DIR="${EROOT%/}/var/lib/postgresql/${SLOT}/data"
359 # environment.bz2 may not contain the same locale as the current system
360 # locale. Unset and source from the current system locale.
361 if [ -f "${EROOT%/}/etc/env.d/02locale" ]; then
370 source "${EROOT%/}/etc/env.d/02locale"
371 [ -n "${LANG}" ] && export LANG
372 [ -n "${LC_CTYPE}" ] && export LC_CTYPE
373 [ -n "${LC_NUMERIC}" ] && export LC_NUMERIC
374 [ -n "${LC_TIME}" ] && export LC_TIME
375 [ -n "${LC_COLLATE}" ] && export LC_COLLATE
376 [ -n "${LC_MONETARY}" ] && export LC_MONETARY
377 [ -n "${LC_MESSAGES}" ] && export LC_MESSAGES
378 [ -n "${LC_ALL}" ] && export LC_ALL
381 einfo "You can modify the paths and options passed to initdb by editing:"
382 einfo " ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
384 einfo "Information on options that can be passed to initdb are found at:"
385 einfo " https://www.postgresql.org/docs/${SLOT}/static/creating-cluster.html"
386 einfo " https://www.postgresql.org/docs/${SLOT}/static/app-initdb.html"
388 einfo "PG_INITDB_OPTS is currently set to:"
389 if [[ -z "${PG_INITDB_OPTS}" ]] ; then
392 einfo " ${PG_INITDB_OPTS}"
395 einfo "Configuration files will be installed to:"
398 einfo "The database cluster will be created in:"
402 ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
406 if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
407 eerror "The given directory, '${DATA_DIR}', is not empty."
408 eerror "Modify DATA_DIR to point to an empty directory."
409 die "${DATA_DIR} is not empty."
412 einfo "Creating the data directory ..."
413 if [[ ${EUID} == 0 ]] ; then
414 mkdir -p "${DATA_DIR}"
415 chown -Rf postgres:postgres "${DATA_DIR}"
416 chmod 0700 "${DATA_DIR}"
419 einfo "Initializing the database ..."
421 if [[ ${EUID} == 0 ]] ; then
422 su postgres -c "${EROOT%/}/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -D \"${DATA_DIR}\" ${PG_INITDB_OPTS}"
424 "${EROOT%/}"/usr/$(get_libdir)/postgresql-${SLOT}/bin/initdb -U postgres -D "${DATA_DIR}" ${PG_INITDB_OPTS}
427 if [[ "${DATA_DIR%/}" != "${PGDATA%/}" ]] ; then
428 mv "${DATA_DIR%/}"/{pg_{hba,ident},postgresql}.conf "${PGDATA}"
429 ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf "${DATA_DIR%/}"
432 # unix_socket_directory has no effect in postgresql.conf as it's
433 # overridden in the initscript
434 sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
436 cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
437 # This is here because of https://bugs.gentoo.org/show_bug.cgi?id=518522
438 # On the off-chance that you might need to work with UTF-8 encoded
439 # characters in PL/Perl
440 plperl.on_init = 'use utf8; use re; package utf8; require "utf8_heavy.pl";'
443 einfo "The autovacuum function, which was in contrib, has been moved to the main"
444 einfo "PostgreSQL functions starting with 8.1, and starting with 8.4 is now enabled"
445 einfo "by default. You can disable it in the cluster's:"
446 einfo " ${PGDATA%/}/postgresql.conf"
448 einfo "The PostgreSQL server, by default, will log events to:"
449 einfo " ${DATA_DIR%/}/postmaster.log"
452 einfo "The location of the configuration files have moved to:"
454 einfo "To start the server:"
455 einfo " pg_ctl start -D ${DATA_DIR} -o '-D ${PGDATA} --data-directory=${DATA_DIR}'"
457 einfo " pg_ctl stop -D ${DATA_DIR}"
459 einfo "Or move the configuration files back:"
460 einfo "mv ${PGDATA}*.conf ${DATA_DIR}"
462 einfo "You should use the '${EROOT%/}/etc/init.d/postgresql-${SLOT}' script to run PostgreSQL"
463 einfo "instead of 'pg_ctl'."
468 if use server && [[ ${UID} -ne 0 ]] ; then
471 einfo "If you think other tests besides the regression tests are necessary, please"
472 einfo "submit a bug including a patch for this ebuild to enable them."
475 ewarn 'Tests cannot be run without the "server" use flag enabled.'
476 [[ ${UID} -eq 0 ]] || \
477 ewarn 'Tests cannot be run as root. Enable "userpriv" in FEATURES.'