You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

458 lines
13 KiB

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