commit:     f1b07f8816c2f0346d07468bdb4c5b9ce4ffada7
Author:     Aaron W. Swenson <titanofold <AT> gentoo <DOT> org>
AuthorDate: Mon Apr 17 13:09:56 2017 +0000
Commit:     Aaron Swenson <titanofold <AT> gentoo <DOT> org>
CommitDate: Mon Apr 17 15:40:58 2017 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=f1b07f88

dev-db/postgresql: Eselect and security related fixes

Dependency bumped on app-eselect/eselect-postgresql to 2.0. Some of
its work has been shifted into the ebuild as the files/links don’t
change until this package is reemerge, unmerged, or updated.

Security issues addressed in the initscripts per bugs 603716 and 603720.

Bugs: 603716, 603720

Package-Manager: portage-2.3.0

 dev-db/postgresql/files/postgresql.confd-9.2       |  65 +++++
 dev-db/postgresql/files/postgresql.confd-9.3       |  65 +++++
 dev-db/postgresql/files/postgresql.init-9.2        | 153 ++++++++++++
 dev-db/postgresql/files/postgresql.init-9.3-r1     | 158 ++++++++++++
 ...sql-9999.ebuild => postgresql-9.2.20-r1.ebuild} | 239 +++++++++++-------
 ...sql-9999.ebuild => postgresql-9.3.16-r1.ebuild} | 236 +++++++++++-------
 ...sql-9999.ebuild => postgresql-9.4.11-r1.ebuild} | 272 +++++++++++++-------
 ...esql-9999.ebuild => postgresql-9.5.6-r1.ebuild} | 276 ++++++++++++++-------
 ...esql-9999.ebuild => postgresql-9.6.2-r1.ebuild} | 273 +++++++++++++-------
 dev-db/postgresql/postgresql-9999.ebuild           | 101 +++++---
 10 files changed, 1338 insertions(+), 500 deletions(-)

diff --git a/dev-db/postgresql/files/postgresql.confd-9.2 
b/dev-db/postgresql/files/postgresql.confd-9.2
new file mode 100644
index 00000000000..7753eeae3a8
--- /dev/null
+++ b/dev-db/postgresql/files/postgresql.confd-9.2
@@ -0,0 +1,65 @@
+# Directory that contains the unix socket. Created and controlled by
+# the related initscript. The directory created will be owned
+# root:postgres with mode 1775.
+#
+# /run/postgresql is the default directory.
+PG_SOCKET_DIRECTORY="/run/postgresql"
+
+# Which port and socket to bind PostgreSQL
+PGPORT="5432"
+
+# How long to wait for server to start in seconds
+START_TIMEOUT=10
+
+# NICE_QUIT ignores new connections and wait for clients to disconnect from
+# server before shutting down. NICE_TIMEOUT in seconds determines how long to
+# wait for this to succeed.
+NICE_TIMEOUT=60
+
+# Forecfully disconnect clients from server and shut down. This is performed
+# after NICE_QUIT. Terminated client connections have their open transactions
+# rolled back.
+# Set RUDE_QUIT to "NO" to disable. RUDE_TIMEOUT in seconds.
+RUDE_QUIT="YES"
+RUDE_TIMEOUT=30
+
+# If the server still fails to shutdown, you can force it to quit by setting
+# this to YES and a recover-run will execute on the next startup.
+# Set FORCE_QUIT to "YES" to enable. FORCE_TIMEOUT in seconds.
+FORCE_QUIT="NO"
+FORCE_TIMEOUT=2
+
+# Extra options to run postmaster with, e.g.:
+# -N is the maximal number of client connections
+# -B is the number of shared buffers and has to be at least 2x the value for -N
+# Please read the man-page to postmaster for more options. Many of these
+# options can be set directly in the configuration file.
+#PGOPTS="-N 512 -B 1024"
+
+# Pass extra environment variables. If you have to export environment variables
+# for the database process, this can be done here.
+# Don't forget to escape quotes.
+#PG_EXTRA_ENV="PGPASSFILE=\"/path/to/.pgpass\""
+
+##############################################################################
+#
+# The following values should not be arbitrarily changed.
+#
+# `emerge --config dev-db/postgresql:@SLOT@' uses these values to
+# determine where to create the data directory, where to place the
+# configuration files, and any additional options to pass to initdb.
+#
+# The initscript also uses these variables to inform PostgreSQL where to find
+# its data directory and configuration files.
+#
+##############################################################################
+
+# Location of configuration files
+PGDATA="/etc/postgresql-@SLOT@/"
+
+# Where the data directory is located/to be created
+DATA_DIR="/var/lib/postgresql/@SLOT@/data"
+
+# Additional options to pass to initdb.
+# See `man initdb' for available options.
+PG_INITDB_OPTS="--encoding=UTF8"

diff --git a/dev-db/postgresql/files/postgresql.confd-9.3 
b/dev-db/postgresql/files/postgresql.confd-9.3
new file mode 100644
index 00000000000..8b6d2a09728
--- /dev/null
+++ b/dev-db/postgresql/files/postgresql.confd-9.3
@@ -0,0 +1,65 @@
+# Comma-separated list of directories that contain a unix
+# socket. Created and controlled by the related initscript. The
+# directories created will be owned root:postgres with mode 1775.
+#
+# /run/postgresql is the default directory.
+PG_SOCKET_DIRECTORIES="/run/postgresql"
+
+# Which port and socket to bind PostgreSQL
+PGPORT="5432"
+
+# How long to wait for server to start in seconds
+START_TIMEOUT=10
+
+# NICE_QUIT ignores new connections and wait for clients to disconnect from
+# server before shutting down. NICE_TIMEOUT in seconds determines how long to
+# wait for this to succeed.
+NICE_TIMEOUT=60
+
+# Forecfully disconnect clients from server and shut down. This is performed
+# after NICE_QUIT. Terminated client connections have their open transactions
+# rolled back.
+# Set RUDE_QUIT to "NO" to disable. RUDE_TIMEOUT in seconds.
+RUDE_QUIT="YES"
+RUDE_TIMEOUT=30
+
+# If the server still fails to shutdown, you can force it to quit by setting
+# this to YES and a recover-run will execute on the next startup.
+# Set FORCE_QUIT to "YES" to enable. FORCE_TIMEOUT in seconds.
+FORCE_QUIT="NO"
+FORCE_TIMEOUT=2
+
+# Extra options to run postmaster with, e.g.:
+# -N is the maximal number of client connections
+# -B is the number of shared buffers and has to be at least 2x the value for -N
+# Please read the man-page to postmaster for more options. Many of these
+# options can be set directly in the configuration file.
+#PGOPTS="-N 512 -B 1024"
+
+# Pass extra environment variables. If you have to export environment variables
+# for the database process, this can be done here.
+# Don't forget to escape quotes.
+#PG_EXTRA_ENV="PGPASSFILE=\"/path/to/.pgpass\""
+
+##############################################################################
+#
+# The following values should not be arbitrarily changed.
+#
+# `emerge --config dev-db/postgresql:@SLOT@' uses these values to
+# determine where to create the data directory, where to place the
+# configuration files, and any additional options to pass to initdb.
+#
+# The initscript also uses these variables to inform PostgreSQL where to find
+# its data directory and configuration files.
+#
+##############################################################################
+
+# Location of configuration files
+PGDATA="/etc/postgresql-@SLOT@/"
+
+# Where the data directory is located/to be created
+DATA_DIR="/var/lib/postgresql/@SLOT@/data"
+
+# Additional options to pass to initdb.
+# See `man initdb' for available options.
+PG_INITDB_OPTS="--encoding=UTF8"

diff --git a/dev-db/postgresql/files/postgresql.init-9.2 
b/dev-db/postgresql/files/postgresql.init-9.2
new file mode 100755
index 00000000000..0b257049008
--- /dev/null
+++ b/dev-db/postgresql/files/postgresql.init-9.2
@@ -0,0 +1,153 @@
+#!/sbin/openrc-run
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+extra_started_commands="reload promote"
+
+PG_CTL="/usr/@LIBDIR@/postgresql-@SLOT@/bin/pg_ctl"
+
+description="PostgreSQL @SLOT@ -- the world's most advanced open source 
database --
+${RC_SERVICE} is a wrapper around pg_ctl with additional administrative checks
+and convenience"
+
+get_config() {
+    [ -f "${PGDATA%/}/postgresql.conf" ] || return 1
+
+    eval echo $(sed -e 's:#.*::' "${PGDATA%/}/postgresql.conf" \
+        | awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }')
+}
+
+depend() {
+    use net
+    provide postgresql
+
+    if [ "$(get_config log_destination)" = "syslog" ]; then
+        use logger
+    fi
+}
+
+configured_port=$(get_config port)
+: ${configured_port:=${PGPORT}}
+
+checkconfig() {
+    # Check that DATA_DIR has been set
+    if [ -z "${DATA_DIR}" ] ; then
+        eerror "DATA_DIR not set"
+        eerror "HINT: Perhaps you need to update /etc/conf.d/postgresql-@SLOT@"
+        return 1
+    fi
+
+    # Check that DATA_DIR exists
+    if [ ! -d "${DATA_DIR}" ] ; then
+        eerror "Directory not found: ${DATA_DIR}"
+        eerror "HINT: Ensure that DATA_DIR points to the right path."
+        eerror "HINT: Or perhaps you need to create the database cluster:"
+        eerror "    emerge --config dev-db/postgresql:@SLOT@"
+        return 1
+    fi
+
+    # Check for the existence of PostgreSQL's config files, and set the
+    # proper mode and ownership.
+    # Only three files should be checked as potentially other files
+    # may be in PGDATA that should not be touched.
+    local file
+    for file in postgresql pg_hba pg_ident ; do
+        file="${PGDATA%/}/${file}.conf"
+        if [ -f "${file}" ] ; then
+            checkpath -f -m 0600 -o postgres:postgres "${file}"
+        else
+            eerror "${file} not found"
+            eerror "HINT: mv ${DATA_DIR%/}/*.conf ${PGDATA}"
+            return 1
+        fi
+    done
+
+    # Set the proper permission for the socket path and create it if
+    # it doesn't exist.
+    checkpath -d -m 1775 -o root:postgres "${PG_SOCKET_DIRECTORY}"
+    if [ -e "${PG_SOCKET_DIRECTORY%/}/.s.PGSQL.${configured_port}" ] ; then
+        eerror "Socket conflict."
+        eerror "A server is already listening on:"
+        eerror "    ${PG_SOCKET_DIRECTORY%/}/.s.PGSQL.${configured_port}"
+        eerror "HINT: Change PGPORT to listen on a different socket."
+        return 1
+    fi
+}
+
+start() {
+    checkconfig || return 1
+
+    ebegin "Starting PostgreSQL @SLOT@"
+
+    rm -f "${DATA_DIR%/}/postmaster.pid"
+
+    su - postgres -c \
+       "PGPORT=${configured_port} ${PG_EXTRA_ENV} ${PG_CTL} start \
+           -s -w -t ${START_TIMEOUT} -l ${DATA_DIR%/}/postmaster.log \
+           -D ${PGDATA} \
+           -o '--data-directory=${DATA_DIR} \
+               --unix-socket-directory=${PG_SOCKET_DIRECTORY} \
+               ${PGOPTS}'"
+
+    local retval=$?
+
+    if [ $retval -ne 0 ] ; then
+        eerror "Check the log for a possible explanation of the above error."
+        eerror "The log may be located at:"
+        eerror "    ${DATA_DIR%/}/postmaster.log"
+        eerror "Or wherever you configured PostgreSQL @SLOT@ to log."
+    fi
+
+    eend $retval
+}
+
+stop() {
+    local seconds=$(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} ))
+    ebegin "Stopping PostgreSQL @SLOT@ (this can take up to ${seconds} 
seconds)"
+
+    su - postgres -c \
+       "${PG_CTL} stop -t ${NICE_TIMEOUT} -s -D ${DATA_DIR} -m smart"
+    local retval=$?
+
+    if [ "${RUDE_QUIT}" != "NO" -a ${retval} -ne 0 ] ; then
+        einfo "Previous attempt failed. Trying RUDE_QUIT."
+        su - postgres -c \
+           "${PG_CTL} stop -t ${RUDE_TIMEOUT} -s -D ${DATA_DIR} -m fast"
+        retval=$?
+    fi
+
+    if [ "${FORCE_QUIT}" = "YES" -a ${retval} -ne 0 ] ; then
+        einfo "Previous step failed. Trying FORCE_QUIT."
+        ewarn "A recover-run might be executed on next startup."
+        su - postgres -c \
+           "${PG_CTL} stop -t ${FORCE_TIMEOUT} -s -D ${DATA_DIR} -m immediate"
+        retval=$?
+    fi
+
+    eend ${retval}
+}
+
+status() {
+    ebegin "Checking PostgreSQL @SLOT@ status"
+    su - postgres -c "${PG_CTL} status -D ${DATA_DIR}"
+    eend $?
+}
+
+description_reload="Simply sends the postgres process a SIGHUP signal, causing
+           it to reread its configuration files (postgresql.conf, pg_hba.conf,
+           etc.). This allows changing of configuration-file options that do 
not
+           require a complete restart to take effect."
+reload() {
+    ebegin "Reloading PostgreSQL @SLOT@ configuration"
+    su - postgres -c "${PG_CTL} reload -s -D ${DATA_DIR}"
+    eend $?
+}
+
+description_promote="If the server is in standby, it is commanded to exit
+            recovery and begin read-write operations."
+promote() {
+    ebegin "Promoting PostgreSQL @SLOT@"
+    su - postgres -c "${PG_CTL} promote -s -D ${DATA_DIR}"
+    eend $?
+}

diff --git a/dev-db/postgresql/files/postgresql.init-9.3-r1 
b/dev-db/postgresql/files/postgresql.init-9.3-r1
new file mode 100755
index 00000000000..b7a08990bf6
--- /dev/null
+++ b/dev-db/postgresql/files/postgresql.init-9.3-r1
@@ -0,0 +1,158 @@
+#!/sbin/openrc-run
+# Copyright 1999-2017 Gentoo Foundation
+# Distributed under the terms of the GNU General Public License v2
+# $Id$
+
+extra_started_commands="reload promote"
+
+PG_CTL="/usr/@LIBDIR@/postgresql-@SLOT@/bin/pg_ctl"
+
+description="PostgreSQL @SLOT@ -- the world's most advanced open source 
database --
+${RC_SERVICE} is a wrapper around pg_ctl with additional administrative checks
+and convenience"
+
+get_config() {
+    [ -f "${PGDATA%/}/postgresql.conf" ] || return 1
+
+    eval echo $(sed -e 's:#.*::' "${PGDATA%/}/postgresql.conf" \
+        | awk '$1 == "'$1'" { print ($2 == "=" ? $3 : $2) }')
+}
+
+depend() {
+    use net
+    provide postgresql
+
+    if [ "$(get_config log_destination)" = "syslog" ]; then
+        use logger
+    fi
+}
+
+configured_port=$(get_config port)
+: ${configured_port:=${PGPORT}}
+
+checkconfig() {
+    # Check that DATA_DIR has been set
+    if [ -z "${DATA_DIR}" ] ; then
+        eerror "DATA_DIR not set"
+        eerror "HINT: Perhaps you need to update /etc/conf.d/postgresql-@SLOT@"
+        return 1
+    fi
+
+    # Check that DATA_DIR exists
+    if [ ! -d "${DATA_DIR}" ] ; then
+        eerror "Directory not found: ${DATA_DIR}"
+        eerror "HINT: Ensure that DATA_DIR points to the right path."
+        eerror "HINT: Or perhaps you need to create the database cluster:"
+        eerror "    emerge --config dev-db/postgresql:@SLOT@"
+        return 1
+    fi
+
+    # Check for the existence of PostgreSQL's config files, and set the
+    # proper mode and ownership.
+    # Only three files should be checked as potentially other files
+    # may be in PGDATA that should not be touched.
+    local file
+    for file in postgresql pg_hba pg_ident ; do
+        file="${PGDATA%/}/${file}.conf"
+        if [ -f "${file}" ] ; then
+            checkpath -f -m 0600 -o postgres:postgres "${file}"
+        else
+            eerror "${file} not found"
+            eerror "HINT: mv ${DATA_DIR%/}/*.conf ${PGDATA}"
+            return 1
+        fi
+    done
+
+    # Set the proper permission for the socket paths and create it if
+    # it doesn't exist.
+    set -f; IFS=','
+    local s
+    for s in ${PG_SOCKET_DIRECTORIES}; do
+        checkpath -d -m 1775 -o root:postgres "${s}"
+        if [ -e "${s%/}/.s.PGSQL.${configured_port}" ] ; then
+            eerror "Socket conflict."
+            eerror "A server is already listening on:"
+            eerror "    ${s%/}/.s.PGSQL.${configured_port}"
+            eerror "HINT: Change PGPORT to listen on a different socket."
+            return 1
+        fi
+    done
+    set +f; unset IFS
+}
+
+start() {
+    checkconfig || return 1
+
+    ebegin "Starting PostgreSQL @SLOT@"
+
+    rm -f "${DATA_DIR%/}/postmaster.pid"
+
+    su - postgres -c \
+       "PGPORT=${configured_port} ${PG_EXTRA_ENV} ${PG_CTL} start \
+           -s -w -t ${START_TIMEOUT} -l ${DATA_DIR%/}/postmaster.log \
+           -D ${PGDATA} \
+           -o '--data-directory=${DATA_DIR} \
+               --unix-socket-directories=${PG_SOCKET_DIRECTORIES} \
+               ${PGOPTS}'"
+
+    local retval=$?
+
+    if [ $retval -ne 0 ] ; then
+        eerror "Check the log for a possible explanation of the above error."
+        eerror "The log may be located at:"
+        eerror "    ${DATA_DIR%/}/postmaster.log"
+        eerror "Or wherever you configured PostgreSQL @SLOT@ to log."
+    fi
+
+    eend $retval
+}
+
+stop() {
+    local seconds=$(( ${NICE_TIMEOUT} + ${RUDE_TIMEOUT} + ${FORCE_TIMEOUT} ))
+    ebegin "Stopping PostgreSQL @SLOT@ (this can take up to ${seconds} 
seconds)"
+
+    su - postgres -c \
+       "${PG_CTL} stop -t ${NICE_TIMEOUT} -s -D ${DATA_DIR} -m smart"
+    local retval=$?
+
+    if [ "${RUDE_QUIT}" != "NO" -a ${retval} -ne 0 ] ; then
+        einfo "Previous attempt failed. Trying RUDE_QUIT."
+        su - postgres -c \
+           "${PG_CTL} stop -t ${RUDE_TIMEOUT} -s -D ${DATA_DIR} -m fast"
+        retval=$?
+    fi
+
+    if [ "${FORCE_QUIT}" = "YES" -a ${retval} -ne 0 ] ; then
+        einfo "Previous step failed. Trying FORCE_QUIT."
+        ewarn "A recover-run might be executed on next startup."
+        su - postgres -c \
+           "${PG_CTL} stop -t ${FORCE_TIMEOUT} -s -D ${DATA_DIR} -m immediate"
+        retval=$?
+    fi
+
+    eend ${retval}
+}
+
+status() {
+    ebegin "Checking PostgreSQL @SLOT@ status"
+    su - postgres -c "${PG_CTL} status -D ${DATA_DIR}"
+    eend $?
+}
+
+description_reload="Simply sends the postgres process a SIGHUP signal, causing
+           it to reread its configuration files (postgresql.conf, pg_hba.conf,
+           etc.). This allows changing of configuration-file options that do 
not
+           require a complete restart to take effect."
+reload() {
+    ebegin "Reloading PostgreSQL @SLOT@ configuration"
+    su - postgres -c "${PG_CTL} reload -s -D ${DATA_DIR}"
+    eend $?
+}
+
+description_promote="If the server is in standby, it is commanded to exit
+            recovery and begin read-write operations."
+promote() {
+    ebegin "Promoting PostgreSQL @SLOT@"
+    su - postgres -c "${PG_CTL} promote -s -D ${DATA_DIR}"
+    eend $?
+}

diff --git a/dev-db/postgresql/postgresql-9999.ebuild 
b/dev-db/postgresql/postgresql-9.2.20-r1.ebuild
similarity index 58%
copy from dev-db/postgresql/postgresql-9999.ebuild
copy to dev-db/postgresql/postgresql-9.2.20-r1.ebuild
index 6a995c154ba..09d00f4a78a 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9.2.20-r1.ebuild
@@ -3,17 +3,16 @@
 
 EAPI="5"
 
-PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
+PYTHON_COMPAT=( python{2_7,3_4} )
 
-inherit eutils flag-o-matic git-2 linux-info multilib pam prefix \
-               python-single-r1 systemd user versionator
+inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
+               systemd user versionator
 
-KEYWORDS=""
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc 
~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-# Fix if needed
-SLOT="9.7"
+SLOT="$(get_version_component_range 1-2)"
 
-EGIT_REPO_URI="git://git.postgresql.org/git/postgresql.git"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
@@ -21,7 +20,7 @@ HOMEPAGE="http://www.postgresql.org/";
 
 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
                 zh_CN zh_TW"
-IUSE="kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
          +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
@@ -39,7 +38,7 @@ wanted_languages() {
 }
 
 CDEPEND="
->=app-eselect/eselect-postgresql-1.2.0
+>=app-eselect/eselect-postgresql-2.0
 sys-apps/less
 virtual/libintl
 kerberos? ( virtual/krb5 )
@@ -60,23 +59,11 @@ zlib? ( sys-libs/zlib )
 
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
->=dev-lang/perl-5.8
-app-text/docbook-dsssl-stylesheets
-app-text/docbook-sgml-dtd:4.2
-app-text/docbook-xml-dtd:4.2
-app-text/docbook-xsl-stylesheets
-app-text/openjade
-dev-libs/libxml2
-dev-libs/libxslt
 sys-devel/bison
 sys-devel/flex
 nls? ( sys-devel/gettext )
 xml? ( virtual/pkgconfig )
 "
-src_unpack() {
-       base_src_unpack
-       git-2_src_unpack
-}
 
 RDEPEND="${CDEPEND}
 !dev-db/postgresql-docs:${SLOT}
@@ -85,17 +72,8 @@ RDEPEND="${CDEPEND}
 selinux? ( sec-policy/selinux-postgresql )
 "
 
-pkg_pretend() {
-       ewarn "You are using a live ebuild that uses the current source code as 
it is"
-       ewarn "available from PostgreSQL's Git repository at emerge time. Given 
such,"
-       ewarn "the GNU Makefiles may be altered by upstream without notice and 
the"
-       ewarn "documentation for this live version is not readily available"
-       ewarn "online. Ergo, the ebuild maintainers will not support building a"
-       ewarn "client-only and/or document-free version."
-}
-
 pkg_setup() {
-       CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
+       use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
 
        enewgroup postgres 70
        enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
@@ -111,10 +89,11 @@ src_prepare() {
        sed "s|\(PGSOCKET_DIR\s\+\)\"/tmp\"|\1\"${EPREFIX}/run/postgresql\"|" \
                -i src/include/pg_config_manual.h || die
 
-       # Rely on $PATH being in the proper order so that the correct
-       # install program is used for modules utilizing PGXS in both
-       # hardened and non-hardened environments. (Bug #528786)
-       sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
+       use server || epatch "${FILESDIR}/${PN}-${SLOT}-no-server.patch"
+
+       # Fix bug 486556 where the server would crash at start up because of
+       # an infinite loop caused by a self-referencing symlink.
+       epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
 
        if use pam ; then
                sed -e "s/\(#define PGSQL_PAM_SERVICE 
\"postgresql\)/\1-${SLOT}/" \
@@ -148,6 +127,7 @@ src_configure() {
                $(use_enable !pg_legacytimestamp integer-datetimes) \
                $(use_enable threads thread-safety) \
                $(use_with kerberos gssapi) \
+               $(use_with kerberos krb5) \
                $(use_with ldap) \
                $(use_with pam) \
                $(use_with perl) \
@@ -163,78 +143,150 @@ src_configure() {
 }
 
 src_compile() {
-       emake world
+       emake
+       emake -C contrib
 }
 
 src_install() {
-       emake DESTDIR="${D}" install-world
+       emake DESTDIR="${D}" install
+       emake DESTDIR="${D}" install -C contrib
 
        dodoc README HISTORY doc/{TODO,bug.template}
 
+       # man pages are already built, but if we have the target make them,
+       # they'll be generated from source before being installed so we
+       # manually install man pages.
+       # We use ${SLOT} instead of doman for postgresql.eselect
+       insinto /usr/share/postgresql-${SLOT}/man/
+       doins -r doc/src/sgml/man{1,3,7}
+       if ! use server; then
+               # Remove man pages for non-existent binaries
+               for m in 
{initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
+                       rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
+               done
+       fi
+       docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
+
        insinto /etc/postgresql-${SLOT}
        newins src/bin/psql/psqlrc.sample psqlrc
 
-       dodir /etc/eselect/postgresql/slots/${SLOT}
-       echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
-               "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
-
        use static-libs || find "${ED}" -name '*.a' -delete
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
+       local f bn
+       for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
+                                       -mindepth 1 -maxdepth 1)
+       do
+               bn=$(basename "${f}")
+               # Temporarily tack on tmp to workaround a file collision
+               # issue. This is only necessary for 9.7 and earlier. 10 never
+               # had this issue.
+               dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
+                         "/usr/bin/${bn}${SLOT/.}tmp"
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.init" | newinitd - ${PN}-${SLOT}
+       local linkname mansec
+       for mansec in {1,3,7} ; do
+               for f in 
"${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
+                       bn=$(basename "${f}")
+                       linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
+                       dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
+                                 /usr/share/man/man${mansec}/${linkname}
+               done
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.service" | \
-               systemd_newunit - ${PN}-${SLOT}.service
+       if use doc ; then
+               docinto html
+               dodoc doc/src/sgml/html/*
 
-       newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
+               docinto sgml
+               dodoc doc/src/sgml/*.{sgml,dsl}
+       fi
 
-       use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
+       if use server; then
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.confd-9.2" | newconfd - ${PN}-${SLOT}
 
-       if use prefix ; then
-               keepdir /run/postgresql
-               fperms 0775 /run/postgresql
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.init-9.2" | newinitd - ${PN}-${SLOT}
+
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.service" | \
+                       systemd_newunit - ${PN}-${SLOT}.service
+
+               newbin "${FILESDIR}"/${PN}-check-db-dir 
${PN}-${SLOT}-check-db-dir
+
+               use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account 
session
+
+               if use prefix ; then
+                       keepdir /run/postgresql
+                       fperms 0775 /run/postgresql
+               fi
        fi
 }
 
+pkg_preinst() {
+       # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+       # /usr/bin/psql96). They may have been created by the
+       # postgresql.eselect module, but they're handled within this ebuild
+       # now. It's alright if we momentarily delete /usr/bin/psql as it
+       # will be recreated by the eselect module in pkg_ppostinst(). This
+       # is only necessary for 9.7 and earlier. 10 and later were never
+       # handled in this manner.
+       local canonicalise
+       if type -p realpath > /dev/null; then
+               canonicalise=realpath
+       elif type -p readlink > /dev/null; then
+               canonicalise='readlink -f'
+       else
+               # can't die, subshell
+               die "No readlink nor realpath found, cannot canonicalise"
+       fi
+
+       local l
+       # First remove any symlinks in /usr/bin that may have been created
+       # by the old eselect
+       for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; 
do
+               if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; 
then
+                       rm "${l}" || ewarn "Couldn't remove ${l}"
+               fi
+       done
+
+       # Then move the symlinks created by the ebuild to their proper place.
+       for l in "${ED}"/usr/bin/*tmp ; do
+               mv "${l}" "${l%tmp}" \
+                       || ewarn "Couldn't rename $(basename ${l}) to 
$(basename ${l%tmp})"
+       done
+}
+
 pkg_postinst() {
        postgresql-config update
 
        elog "If you need a global psqlrc-file, you can place it in:"
        elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
-       if [[ -z ${REPLACING_VERSIONS} ]] ; then
+       if use server ; then
+               elog
+               elog "Gentoo specific documentation:"
+               elog "https://wiki.gentoo.org/wiki/PostgreSQL";
+               elog
+               elog "Official documentation:"
+               elog "http://www.postgresql.org/docs/${SLOT}/static/index.html";
+               elog
+               elog "The default location of the Unix-domain socket is:"
+               elog "    ${EROOT%/}/run/postgresql/"
                elog
-               elog "It looks like this is your first time installing 
PostgreSQL. Run the"
-               elog "following command in all active shells to pick up changes 
to the default"
+               elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
+               elog "so that it contains your preferred locale in:"
+               elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
+               elog
+               elog "Then, execute the following command to setup the initial 
database"
                elog "environment:"
-               elog "    source /etc/profile"
+               elog "    emerge --config =${CATEGORY}/${PF}"
        fi
-
-       elog
-       elog "Gentoo specific documentation:"
-       elog "https://wiki.gentoo.org/wiki/PostgreSQL";
-       elog
-       elog "Official documentation:"
-       elog "${EROOT%/}/usr/share/doc/${PF}/html"
-       elog
-       elog "The default location of the Unix-domain socket is:"
-       elog "    ${EROOT%/}/run/postgresql/"
-       elog
-       elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
-       elog "so that it contains your preferred locale, and other options, in:"
-       elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
-       elog
-       elog "Then, execute the following command to setup the initial database"
-       elog "environment:"
-       elog "    emerge --config =${CATEGORY}/${PF}"
 }
 
 pkg_prerm() {
-       if [[ -z ${REPLACED_BY_VERSION} ]] ; then
+       if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
                ewarn "Have you dumped and/or migrated the ${SLOT} database 
cluster?"
                ewarn 
"\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL";
 
@@ -249,6 +301,8 @@ pkg_postrm() {
 }
 
 pkg_config() {
+       use server || die "USE flag 'server' not enabled. Nothing to configure."
+
        [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
                && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
        [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
@@ -297,21 +351,10 @@ pkg_config() {
        einfo "The database cluster will be created in:"
        einfo "    ${DATA_DIR}"
        einfo
-       if [ -z "$PG_AUTOCONFIG" ] ; then
-               while [ "$correct" != "true" ] ; do
-                       einfo "Are you ready to continue? (y/n)"
-                       read answer
-                       if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
-                               correct="true"
-                       elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
-                               die "Aborting initialization."
-                       else
-                               echo "Answer not recognized"
-                       fi
-               done
-       else
-               einfo "PG_AUTOCONFIG set, not prompting"
-       fi
+
+       ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
+       sleep 5
+       eend 0
 
        if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
                eerror "The given directory, '${DATA_DIR}', is not empty."
@@ -339,6 +382,10 @@ pkg_config() {
                ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf 
"${DATA_DIR%/}"
        fi
 
+       # unix_socket_directory has no effect in postgresql.conf as it's
+       # overridden in the initscript
+       sed '/^#unix_socket_directory/d' -i "${PGDATA%/}"/postgresql.conf
+
        cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
                # This is here because of 
https://bugs.gentoo.org/show_bug.cgi?id=518522
                # On the off-chance that you might need to work with UTF-8 
encoded
@@ -371,17 +418,17 @@ pkg_config() {
 }
 
 src_test() {
-       einfo ">>> Test phase [check]: ${CATEGORY}/${PF}"
-
-       if [[ ${UID} -ne 0 ]] ; then
+       if use server && [[ ${UID} -ne 0 ]] ; then
                emake check
 
                einfo "If you think other tests besides the regression tests 
are necessary, please"
                einfo "submit a bug including a patch for this ebuild to enable 
them."
        else
+               use server || \
+                       ewarn 'Tests cannot be run without the "server" use 
flag enabled.'
                [[ ${UID} -eq 0 ]] || \
-                       ewarn "Tests cannot be run as root. Enable 'userpriv' 
in FEATURES."
+                       ewarn 'Tests cannot be run as root. Enable "userpriv" 
in FEATURES.'
 
-               ewarn "Skipping."
+               ewarn 'Skipping.'
        fi
 }

diff --git a/dev-db/postgresql/postgresql-9999.ebuild 
b/dev-db/postgresql/postgresql-9.3.16-r1.ebuild
similarity index 59%
copy from dev-db/postgresql/postgresql-9999.ebuild
copy to dev-db/postgresql/postgresql-9.3.16-r1.ebuild
index 6a995c154ba..0055c0e30d9 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9.3.16-r1.ebuild
@@ -3,17 +3,16 @@
 
 EAPI="5"
 
-PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
+PYTHON_COMPAT=( python{2_7,3_4} )
 
-inherit eutils flag-o-matic git-2 linux-info multilib pam prefix \
-               python-single-r1 systemd user versionator
+inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
+               systemd user versionator
 
-KEYWORDS=""
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc 
~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-# Fix if needed
-SLOT="9.7"
+SLOT="$(get_version_component_range 1-2)"
 
-EGIT_REPO_URI="git://git.postgresql.org/git/postgresql.git"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
@@ -21,7 +20,7 @@ HOMEPAGE="http://www.postgresql.org/";
 
 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
                 zh_CN zh_TW"
-IUSE="kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
          +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
@@ -39,7 +38,7 @@ wanted_languages() {
 }
 
 CDEPEND="
->=app-eselect/eselect-postgresql-1.2.0
+>=app-eselect/eselect-postgresql-2.0
 sys-apps/less
 virtual/libintl
 kerberos? ( virtual/krb5 )
@@ -60,23 +59,11 @@ zlib? ( sys-libs/zlib )
 
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
->=dev-lang/perl-5.8
-app-text/docbook-dsssl-stylesheets
-app-text/docbook-sgml-dtd:4.2
-app-text/docbook-xml-dtd:4.2
-app-text/docbook-xsl-stylesheets
-app-text/openjade
-dev-libs/libxml2
-dev-libs/libxslt
 sys-devel/bison
 sys-devel/flex
 nls? ( sys-devel/gettext )
 xml? ( virtual/pkgconfig )
 "
-src_unpack() {
-       base_src_unpack
-       git-2_src_unpack
-}
 
 RDEPEND="${CDEPEND}
 !dev-db/postgresql-docs:${SLOT}
@@ -85,17 +72,8 @@ RDEPEND="${CDEPEND}
 selinux? ( sec-policy/selinux-postgresql )
 "
 
-pkg_pretend() {
-       ewarn "You are using a live ebuild that uses the current source code as 
it is"
-       ewarn "available from PostgreSQL's Git repository at emerge time. Given 
such,"
-       ewarn "the GNU Makefiles may be altered by upstream without notice and 
the"
-       ewarn "documentation for this live version is not readily available"
-       ewarn "online. Ergo, the ebuild maintainers will not support building a"
-       ewarn "client-only and/or document-free version."
-}
-
 pkg_setup() {
-       CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
+       use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
 
        enewgroup postgres 70
        enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
@@ -116,6 +94,12 @@ src_prepare() {
        # hardened and non-hardened environments. (Bug #528786)
        sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
+       use server || epatch "${FILESDIR}/${PN}-${SLOT}-no-server.patch"
+
+       # Fix bug 486556 where the server would crash at start up because of
+       # an infinite loop caused by a self-referencing symlink.
+       epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+
        if use pam ; then
                sed -e "s/\(#define PGSQL_PAM_SERVICE 
\"postgresql\)/\1-${SLOT}/" \
                        -i src/backend/libpq/auth.c || \
@@ -148,6 +132,7 @@ src_configure() {
                $(use_enable !pg_legacytimestamp integer-datetimes) \
                $(use_enable threads thread-safety) \
                $(use_with kerberos gssapi) \
+               $(use_with kerberos krb5) \
                $(use_with ldap) \
                $(use_with pam) \
                $(use_with perl) \
@@ -163,41 +148,119 @@ src_configure() {
 }
 
 src_compile() {
-       emake world
+       emake
+       emake -C contrib
 }
 
 src_install() {
-       emake DESTDIR="${D}" install-world
+       emake DESTDIR="${D}" install
+       emake DESTDIR="${D}" install -C contrib
 
        dodoc README HISTORY doc/{TODO,bug.template}
 
+       # man pages are already built, but if we have the target make them,
+       # they'll be generated from source before being installed so we
+       # manually install man pages.
+       # We use ${SLOT} instead of doman for postgresql.eselect
+       insinto /usr/share/postgresql-${SLOT}/man/
+       doins -r doc/src/sgml/man{1,3,7}
+       if ! use server; then
+               # Remove man pages for non-existent binaries
+               for m in 
{initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
+                       rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
+               done
+       fi
+       docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
+
        insinto /etc/postgresql-${SLOT}
        newins src/bin/psql/psqlrc.sample psqlrc
 
-       dodir /etc/eselect/postgresql/slots/${SLOT}
-       echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
-               "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
-
        use static-libs || find "${ED}" -name '*.a' -delete
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
+       local f bn
+       for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
+                                       -mindepth 1 -maxdepth 1)
+       do
+               bn=$(basename "${f}")
+               # Temporarily tack on tmp to workaround a file collision
+               # issue. This is only necessary for 9.7 and earlier. 10 never
+               # had this issue.
+               dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
+                         "/usr/bin/${bn}${SLOT/.}tmp"
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.init" | newinitd - ${PN}-${SLOT}
+       local linkname mansec
+       for mansec in {1,3,7} ; do
+               for f in 
"${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
+                       bn=$(basename "${f}")
+                       linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
+                       dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
+                                 /usr/share/man/man${mansec}/${linkname}
+               done
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.service" | \
-               systemd_newunit - ${PN}-${SLOT}.service
+       if use doc ; then
+               docinto html
+               dodoc doc/src/sgml/html/*
 
-       newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
+               docinto sgml
+               dodoc doc/src/sgml/*.{sgml,dsl}
+       fi
 
-       use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
+       if use server; then
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
 
-       if use prefix ; then
-               keepdir /run/postgresql
-               fperms 0775 /run/postgresql
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - 
${PN}-${SLOT}
+
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.service" | \
+                       systemd_newunit - ${PN}-${SLOT}.service
+
+               newbin "${FILESDIR}"/${PN}-check-db-dir 
${PN}-${SLOT}-check-db-dir
+
+               use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account 
session
+
+               if use prefix ; then
+                       keepdir /run/postgresql
+                       fperms 0775 /run/postgresql
+               fi
+       fi
+}
+
+pkg_preinst() {
+       # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+       # /usr/bin/psql96). They may have been created by the
+       # postgresql.eselect module, but they're handled within this ebuild
+       # now. It's alright if we momentarily delete /usr/bin/psql as it
+       # will be recreated by the eselect module in pkg_ppostinst(). This
+       # is only necessary for 9.7 and earlier. 10 and later were never
+       # handled in this manner.
+       local canonicalise
+       if type -p realpath > /dev/null; then
+               canonicalise=realpath
+       elif type -p readlink > /dev/null; then
+               canonicalise='readlink -f'
+       else
+               # can't die, subshell
+               die "No readlink nor realpath found, cannot canonicalise"
        fi
+
+       local l
+       # First remove any symlinks in /usr/bin that may have been created
+       # by the old eselect
+       for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; 
do
+               if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; 
then
+                       rm "${l}" || ewarn "Couldn't remove ${l}"
+               fi
+       done
+
+       # Then move the symlinks created by the ebuild to their proper place.
+       for l in "${ED}"/usr/bin/*tmp ; do
+               mv "${l}" "${l%tmp}" \
+                       || ewarn "Couldn't rename $(basename ${l}) to 
$(basename ${l%tmp})"
+       done
 }
 
 pkg_postinst() {
@@ -206,35 +269,29 @@ pkg_postinst() {
        elog "If you need a global psqlrc-file, you can place it in:"
        elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
-       if [[ -z ${REPLACING_VERSIONS} ]] ; then
+       if use server ; then
+               elog
+               elog "Gentoo specific documentation:"
+               elog "https://wiki.gentoo.org/wiki/PostgreSQL";
+               elog
+               elog "Official documentation:"
+               elog "http://www.postgresql.org/docs/${SLOT}/static/index.html";
+               elog
+               elog "The default location of the Unix-domain socket is:"
+               elog "    ${EROOT%/}/run/postgresql/"
                elog
-               elog "It looks like this is your first time installing 
PostgreSQL. Run the"
-               elog "following command in all active shells to pick up changes 
to the default"
+               elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
+               elog "so that it contains your preferred locale in:"
+               elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
+               elog
+               elog "Then, execute the following command to setup the initial 
database"
                elog "environment:"
-               elog "    source /etc/profile"
+               elog "    emerge --config =${CATEGORY}/${PF}"
        fi
-
-       elog
-       elog "Gentoo specific documentation:"
-       elog "https://wiki.gentoo.org/wiki/PostgreSQL";
-       elog
-       elog "Official documentation:"
-       elog "${EROOT%/}/usr/share/doc/${PF}/html"
-       elog
-       elog "The default location of the Unix-domain socket is:"
-       elog "    ${EROOT%/}/run/postgresql/"
-       elog
-       elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
-       elog "so that it contains your preferred locale, and other options, in:"
-       elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
-       elog
-       elog "Then, execute the following command to setup the initial database"
-       elog "environment:"
-       elog "    emerge --config =${CATEGORY}/${PF}"
 }
 
 pkg_prerm() {
-       if [[ -z ${REPLACED_BY_VERSION} ]] ; then
+       if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
                ewarn "Have you dumped and/or migrated the ${SLOT} database 
cluster?"
                ewarn 
"\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL";
 
@@ -249,6 +306,8 @@ pkg_postrm() {
 }
 
 pkg_config() {
+       use server || die "USE flag 'server' not enabled. Nothing to configure."
+
        [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
                && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
        [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
@@ -297,21 +356,10 @@ pkg_config() {
        einfo "The database cluster will be created in:"
        einfo "    ${DATA_DIR}"
        einfo
-       if [ -z "$PG_AUTOCONFIG" ] ; then
-               while [ "$correct" != "true" ] ; do
-                       einfo "Are you ready to continue? (y/n)"
-                       read answer
-                       if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
-                               correct="true"
-                       elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
-                               die "Aborting initialization."
-                       else
-                               echo "Answer not recognized"
-                       fi
-               done
-       else
-               einfo "PG_AUTOCONFIG set, not prompting"
-       fi
+
+       ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
+       sleep 5
+       eend 0
 
        if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
                eerror "The given directory, '${DATA_DIR}', is not empty."
@@ -339,6 +387,10 @@ pkg_config() {
                ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf 
"${DATA_DIR%/}"
        fi
 
+       # unix_socket_directory has no effect in postgresql.conf as it's
+       # overridden in the initscript
+       sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
+
        cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
                # This is here because of 
https://bugs.gentoo.org/show_bug.cgi?id=518522
                # On the off-chance that you might need to work with UTF-8 
encoded
@@ -371,17 +423,17 @@ pkg_config() {
 }
 
 src_test() {
-       einfo ">>> Test phase [check]: ${CATEGORY}/${PF}"
-
-       if [[ ${UID} -ne 0 ]] ; then
+       if use server && [[ ${UID} -ne 0 ]] ; then
                emake check
 
                einfo "If you think other tests besides the regression tests 
are necessary, please"
                einfo "submit a bug including a patch for this ebuild to enable 
them."
        else
+               use server || \
+                       ewarn 'Tests cannot be run without the "server" use 
flag enabled.'
                [[ ${UID} -eq 0 ]] || \
-                       ewarn "Tests cannot be run as root. Enable 'userpriv' 
in FEATURES."
+                       ewarn 'Tests cannot be run as root. Enable "userpriv" 
in FEATURES.'
 
-               ewarn "Skipping."
+               ewarn 'Skipping.'
        fi
 }

diff --git a/dev-db/postgresql/postgresql-9999.ebuild 
b/dev-db/postgresql/postgresql-9.4.11-r1.ebuild
similarity index 55%
copy from dev-db/postgresql/postgresql-9999.ebuild
copy to dev-db/postgresql/postgresql-9.4.11-r1.ebuild
index 6a995c154ba..538724b3677 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9.4.11-r1.ebuild
@@ -3,17 +3,16 @@
 
 EAPI="5"
 
-PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
+PYTHON_COMPAT=( python{2_7,3_4} )
 
-inherit eutils flag-o-matic git-2 linux-info multilib pam prefix \
-               python-single-r1 systemd user versionator
+inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
+               systemd user versionator
 
-KEYWORDS=""
+KEYWORDS="~alpha ~amd64 ~arm ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh ~sparc 
~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-# Fix if needed
-SLOT="9.7"
+SLOT="$(get_version_component_range 1-2)"
 
-EGIT_REPO_URI="git://git.postgresql.org/git/postgresql.git"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
@@ -21,7 +20,7 @@ HOMEPAGE="http://www.postgresql.org/";
 
 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
                 zh_CN zh_TW"
-IUSE="kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
          +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
@@ -39,7 +38,7 @@ wanted_languages() {
 }
 
 CDEPEND="
->=app-eselect/eselect-postgresql-1.2.0
+>=app-eselect/eselect-postgresql-2.0
 sys-apps/less
 virtual/libintl
 kerberos? ( virtual/krb5 )
@@ -53,30 +52,40 @@ ssl? (
        libressl? ( dev-libs/libressl:= )
 )
 tcl? ( >=dev-lang/tcl-8:0= )
-uuid? ( dev-libs/ossp-uuid )
 xml? ( dev-libs/libxml2 dev-libs/libxslt )
 zlib? ( sys-libs/zlib )
 "
 
+# uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
+# supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
+# the libc includes UUID functions.
+UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
+BSD_LIBC=( elibc_{Free,Net,Open}BSD )
+
+nest_usedep() {
+       local front back
+       while [[ ${#} -gt 1 ]]; do
+               front+="${1}? ( "
+               back+=" )"
+               shift
+       done
+       echo "${front}${1}${back}"
+}
+
+IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
+CDEPEND+="
+uuid? (
+       ${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
+       $(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} 
dev-libs/ossp-uuid)
+)"
+
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
->=dev-lang/perl-5.8
-app-text/docbook-dsssl-stylesheets
-app-text/docbook-sgml-dtd:4.2
-app-text/docbook-xml-dtd:4.2
-app-text/docbook-xsl-stylesheets
-app-text/openjade
-dev-libs/libxml2
-dev-libs/libxslt
 sys-devel/bison
 sys-devel/flex
 nls? ( sys-devel/gettext )
 xml? ( virtual/pkgconfig )
 "
-src_unpack() {
-       base_src_unpack
-       git-2_src_unpack
-}
 
 RDEPEND="${CDEPEND}
 !dev-db/postgresql-docs:${SLOT}
@@ -85,17 +94,8 @@ RDEPEND="${CDEPEND}
 selinux? ( sec-policy/selinux-postgresql )
 "
 
-pkg_pretend() {
-       ewarn "You are using a live ebuild that uses the current source code as 
it is"
-       ewarn "available from PostgreSQL's Git repository at emerge time. Given 
such,"
-       ewarn "the GNU Makefiles may be altered by upstream without notice and 
the"
-       ewarn "documentation for this live version is not readily available"
-       ewarn "online. Ergo, the ebuild maintainers will not support building a"
-       ewarn "client-only and/or document-free version."
-}
-
 pkg_setup() {
-       CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
+       use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
 
        enewgroup postgres 70
        enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
@@ -116,6 +116,12 @@ src_prepare() {
        # hardened and non-hardened environments. (Bug #528786)
        sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
+       use server || epatch "${FILESDIR}/${PN}-9.4.10-no-server.patch"
+
+       # Fix bug 486556 where the server would crash at start up because of
+       # an infinite loop caused by a self-referencing symlink.
+       epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+
        if use pam ; then
                sed -e "s/\(#define PGSQL_PAM_SERVICE 
\"postgresql\)/\1-${SLOT}/" \
                        -i src/backend/libpq/auth.c || \
@@ -137,6 +143,17 @@ src_configure() {
 
        local PO="${EPREFIX%/}"
 
+       local i uuid_config=""
+       if use uuid; then
+               for i in ${UTIL_LINUX_LIBC[@]}; do
+                       use ${i} && uuid_config="--with-uuid=e2fs"
+               done
+               for i in ${BSD_LIBC[@]}; do
+                       use ${i} && uuid_config="--with-uuid=bsd"
+               done
+               [[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
+       fi
+
        econf \
                --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
                --datadir="${PO}/usr/share/postgresql-${SLOT}" \
@@ -155,7 +172,7 @@ src_configure() {
                $(use_with readline) \
                $(use_with ssl openssl) \
                $(use_with tcl) \
-               $(use_with uuid ossp-uuid) \
+               ${uuid_config} \
                $(use_with xml libxml) \
                $(use_with xml libxslt) \
                $(use_with zlib) \
@@ -163,41 +180,119 @@ src_configure() {
 }
 
 src_compile() {
-       emake world
+       emake
+       emake -C contrib
 }
 
 src_install() {
-       emake DESTDIR="${D}" install-world
+       emake DESTDIR="${D}" install
+       emake DESTDIR="${D}" install -C contrib
 
        dodoc README HISTORY doc/{TODO,bug.template}
 
+       # man pages are already built, but if we have the target make them,
+       # they'll be generated from source before being installed so we
+       # manually install man pages.
+       # We use ${SLOT} instead of doman for postgresql.eselect
+       insinto /usr/share/postgresql-${SLOT}/man/
+       doins -r doc/src/sgml/man{1,3,7}
+       if ! use server; then
+               # Remove man pages for non-existent binaries
+               for m in 
{initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
+                       rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
+               done
+       fi
+       docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
+
        insinto /etc/postgresql-${SLOT}
        newins src/bin/psql/psqlrc.sample psqlrc
 
-       dodir /etc/eselect/postgresql/slots/${SLOT}
-       echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
-               "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
-
        use static-libs || find "${ED}" -name '*.a' -delete
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
+       local f bn
+       for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
+                                       -mindepth 1 -maxdepth 1)
+       do
+               bn=$(basename "${f}")
+               # Temporarily tack on tmp to workaround a file collision
+               # issue. This is only necessary for 9.7 and earlier. 10 never
+               # had this issue.
+               dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
+                         "/usr/bin/${bn}${SLOT/.}tmp"
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.init" | newinitd - ${PN}-${SLOT}
+       local linkname mansec
+       for mansec in {1,3,7} ; do
+               for f in 
"${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
+                       bn=$(basename "${f}")
+                       linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
+                       dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
+                                 /usr/share/man/man${mansec}/${linkname}
+               done
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.service" | \
-               systemd_newunit - ${PN}-${SLOT}.service
+       if use doc ; then
+               docinto html
+               dodoc doc/src/sgml/html/*
 
-       newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
+               docinto sgml
+               dodoc doc/src/sgml/*.{sgml,dsl}
+       fi
 
-       use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
+       if use server; then
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
 
-       if use prefix ; then
-               keepdir /run/postgresql
-               fperms 0775 /run/postgresql
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - 
${PN}-${SLOT}
+
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.service" | \
+                       systemd_newunit - ${PN}-${SLOT}.service
+
+               newbin "${FILESDIR}"/${PN}-check-db-dir 
${PN}-${SLOT}-check-db-dir
+
+               use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account 
session
+
+               if use prefix ; then
+                       keepdir /run/postgresql
+                       fperms 0775 /run/postgresql
+               fi
+       fi
+}
+
+pkg_preinst() {
+       # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+       # /usr/bin/psql96). They may have been created by the
+       # postgresql.eselect module, but they're handled within this ebuild
+       # now. It's alright if we momentarily delete /usr/bin/psql as it
+       # will be recreated by the eselect module in pkg_ppostinst(). This
+       # is only necessary for 9.7 and earlier. 10 and later were never
+       # handled in this manner.
+       local canonicalise
+       if type -p realpath > /dev/null; then
+               canonicalise=realpath
+       elif type -p readlink > /dev/null; then
+               canonicalise='readlink -f'
+       else
+               # can't die, subshell
+               die "No readlink nor realpath found, cannot canonicalise"
        fi
+
+       local l
+       # First remove any symlinks in /usr/bin that may have been created
+       # by the old eselect
+       for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; 
do
+               if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; 
then
+                       rm "${l}" || ewarn "Couldn't remove ${l}"
+               fi
+       done
+
+       # Then move the symlinks created by the ebuild to their proper place.
+       for l in "${ED}"/usr/bin/*tmp ; do
+               mv "${l}" "${l%tmp}" \
+                       || ewarn "Couldn't rename $(basename ${l}) to 
$(basename ${l%tmp})"
+       done
 }
 
 pkg_postinst() {
@@ -206,35 +301,29 @@ pkg_postinst() {
        elog "If you need a global psqlrc-file, you can place it in:"
        elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
-       if [[ -z ${REPLACING_VERSIONS} ]] ; then
+       if use server ; then
+               elog
+               elog "Gentoo specific documentation:"
+               elog "https://wiki.gentoo.org/wiki/PostgreSQL";
                elog
-               elog "It looks like this is your first time installing 
PostgreSQL. Run the"
-               elog "following command in all active shells to pick up changes 
to the default"
+               elog "Official documentation:"
+               elog "http://www.postgresql.org/docs/${SLOT}/static/index.html";
+               elog
+               elog "The default location of the Unix-domain socket is:"
+               elog "    ${EROOT%/}/run/postgresql/"
+               elog
+               elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
+               elog "so that it contains your preferred locale in:"
+               elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
+               elog
+               elog "Then, execute the following command to setup the initial 
database"
                elog "environment:"
-               elog "    source /etc/profile"
+               elog "    emerge --config =${CATEGORY}/${PF}"
        fi
-
-       elog
-       elog "Gentoo specific documentation:"
-       elog "https://wiki.gentoo.org/wiki/PostgreSQL";
-       elog
-       elog "Official documentation:"
-       elog "${EROOT%/}/usr/share/doc/${PF}/html"
-       elog
-       elog "The default location of the Unix-domain socket is:"
-       elog "    ${EROOT%/}/run/postgresql/"
-       elog
-       elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
-       elog "so that it contains your preferred locale, and other options, in:"
-       elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
-       elog
-       elog "Then, execute the following command to setup the initial database"
-       elog "environment:"
-       elog "    emerge --config =${CATEGORY}/${PF}"
 }
 
 pkg_prerm() {
-       if [[ -z ${REPLACED_BY_VERSION} ]] ; then
+       if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
                ewarn "Have you dumped and/or migrated the ${SLOT} database 
cluster?"
                ewarn 
"\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL";
 
@@ -249,6 +338,8 @@ pkg_postrm() {
 }
 
 pkg_config() {
+       use server || die "USE flag 'server' not enabled. Nothing to configure."
+
        [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
                && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
        [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
@@ -297,21 +388,10 @@ pkg_config() {
        einfo "The database cluster will be created in:"
        einfo "    ${DATA_DIR}"
        einfo
-       if [ -z "$PG_AUTOCONFIG" ] ; then
-               while [ "$correct" != "true" ] ; do
-                       einfo "Are you ready to continue? (y/n)"
-                       read answer
-                       if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
-                               correct="true"
-                       elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
-                               die "Aborting initialization."
-                       else
-                               echo "Answer not recognized"
-                       fi
-               done
-       else
-               einfo "PG_AUTOCONFIG set, not prompting"
-       fi
+
+       ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
+       sleep 5
+       eend 0
 
        if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
                eerror "The given directory, '${DATA_DIR}', is not empty."
@@ -339,6 +419,10 @@ pkg_config() {
                ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf 
"${DATA_DIR%/}"
        fi
 
+       # unix_socket_directory has no effect in postgresql.conf as it's
+       # overridden in the initscript
+       sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
+
        cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
                # This is here because of 
https://bugs.gentoo.org/show_bug.cgi?id=518522
                # On the off-chance that you might need to work with UTF-8 
encoded
@@ -371,17 +455,17 @@ pkg_config() {
 }
 
 src_test() {
-       einfo ">>> Test phase [check]: ${CATEGORY}/${PF}"
-
-       if [[ ${UID} -ne 0 ]] ; then
+       if use server && [[ ${UID} -ne 0 ]] ; then
                emake check
 
                einfo "If you think other tests besides the regression tests 
are necessary, please"
                einfo "submit a bug including a patch for this ebuild to enable 
them."
        else
+               use server || \
+                       ewarn 'Tests cannot be run without the "server" use 
flag enabled.'
                [[ ${UID} -eq 0 ]] || \
-                       ewarn "Tests cannot be run as root. Enable 'userpriv' 
in FEATURES."
+                       ewarn 'Tests cannot be run as root. Enable "userpriv" 
in FEATURES.'
 
-               ewarn "Skipping."
+               ewarn 'Skipping.'
        fi
 }

diff --git a/dev-db/postgresql/postgresql-9999.ebuild 
b/dev-db/postgresql/postgresql-9.5.6-r1.ebuild
similarity index 55%
copy from dev-db/postgresql/postgresql-9999.ebuild
copy to dev-db/postgresql/postgresql-9.5.6-r1.ebuild
index 6a995c154ba..fdfc66fd9bd 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9.5.6-r1.ebuild
@@ -5,15 +5,14 @@ EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
-inherit eutils flag-o-matic git-2 linux-info multilib pam prefix \
-               python-single-r1 systemd user versionator
+inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
+               systemd user versionator
 
-KEYWORDS=""
+KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh 
~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-# Fix if needed
-SLOT="9.7"
+SLOT="$(get_version_component_range 1-2)"
 
-EGIT_REPO_URI="git://git.postgresql.org/git/postgresql.git"
+SRC_URI="mirror://postgresql/source/v${PV}/postgresql-${PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
@@ -21,7 +20,7 @@ HOMEPAGE="http://www.postgresql.org/";
 
 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
                 zh_CN zh_TW"
-IUSE="kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
          +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
@@ -39,7 +38,7 @@ wanted_languages() {
 }
 
 CDEPEND="
->=app-eselect/eselect-postgresql-1.2.0
+>=app-eselect/eselect-postgresql-2.0
 sys-apps/less
 virtual/libintl
 kerberos? ( virtual/krb5 )
@@ -53,30 +52,40 @@ ssl? (
        libressl? ( dev-libs/libressl:= )
 )
 tcl? ( >=dev-lang/tcl-8:0= )
-uuid? ( dev-libs/ossp-uuid )
 xml? ( dev-libs/libxml2 dev-libs/libxslt )
 zlib? ( sys-libs/zlib )
 "
 
+# uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
+# supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
+# the libc includes UUID functions.
+UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
+BSD_LIBC=( elibc_{Free,Net,Open}BSD )
+
+nest_usedep() {
+       local front back
+       while [[ ${#} -gt 1 ]]; do
+               front+="${1}? ( "
+               back+=" )"
+               shift
+       done
+       echo "${front}${1}${back}"
+}
+
+IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
+CDEPEND+="
+uuid? (
+       ${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
+       $(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} 
dev-libs/ossp-uuid)
+)"
+
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
->=dev-lang/perl-5.8
-app-text/docbook-dsssl-stylesheets
-app-text/docbook-sgml-dtd:4.2
-app-text/docbook-xml-dtd:4.2
-app-text/docbook-xsl-stylesheets
-app-text/openjade
-dev-libs/libxml2
-dev-libs/libxslt
 sys-devel/bison
 sys-devel/flex
 nls? ( sys-devel/gettext )
 xml? ( virtual/pkgconfig )
 "
-src_unpack() {
-       base_src_unpack
-       git-2_src_unpack
-}
 
 RDEPEND="${CDEPEND}
 !dev-db/postgresql-docs:${SLOT}
@@ -85,17 +94,8 @@ RDEPEND="${CDEPEND}
 selinux? ( sec-policy/selinux-postgresql )
 "
 
-pkg_pretend() {
-       ewarn "You are using a live ebuild that uses the current source code as 
it is"
-       ewarn "available from PostgreSQL's Git repository at emerge time. Given 
such,"
-       ewarn "the GNU Makefiles may be altered by upstream without notice and 
the"
-       ewarn "documentation for this live version is not readily available"
-       ewarn "online. Ergo, the ebuild maintainers will not support building a"
-       ewarn "client-only and/or document-free version."
-}
-
 pkg_setup() {
-       CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
+       use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
 
        enewgroup postgres 70
        enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
@@ -116,6 +116,12 @@ src_prepare() {
        # hardened and non-hardened environments. (Bug #528786)
        sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
+       use server || epatch "${FILESDIR}/${PN}-9.5.5-no-server.patch"
+
+       # Fix bug 486556 where the server would crash at start up because of
+       # an infinite loop caused by a self-referencing symlink.
+       epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+
        if use pam ; then
                sed -e "s/\(#define PGSQL_PAM_SERVICE 
\"postgresql\)/\1-${SLOT}/" \
                        -i src/backend/libpq/auth.c || \
@@ -137,6 +143,17 @@ src_configure() {
 
        local PO="${EPREFIX%/}"
 
+       local i uuid_config=""
+       if use uuid; then
+               for i in ${UTIL_LINUX_LIBC[@]}; do
+                       use ${i} && uuid_config="--with-uuid=e2fs"
+               done
+               for i in ${BSD_LIBC[@]}; do
+                       use ${i} && uuid_config="--with-uuid=bsd"
+               done
+               [[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
+       fi
+
        econf \
                --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
                --datadir="${PO}/usr/share/postgresql-${SLOT}" \
@@ -145,6 +162,7 @@ src_configure() {
                --mandir="${PO}/usr/share/postgresql-${SLOT}/man" \
                --sysconfdir="${PO}/etc/postgresql-${SLOT}" \
                --with-system-tzdata="${PO}/usr/share/zoneinfo" \
+               $(use_enable !alpha spinlocks) \
                $(use_enable !pg_legacytimestamp integer-datetimes) \
                $(use_enable threads thread-safety) \
                $(use_with kerberos gssapi) \
@@ -155,7 +173,7 @@ src_configure() {
                $(use_with readline) \
                $(use_with ssl openssl) \
                $(use_with tcl) \
-               $(use_with uuid ossp-uuid) \
+               ${uuid_config} \
                $(use_with xml libxml) \
                $(use_with xml libxslt) \
                $(use_with zlib) \
@@ -163,78 +181,155 @@ src_configure() {
 }
 
 src_compile() {
-       emake world
+       emake
+       emake -C contrib
 }
 
 src_install() {
-       emake DESTDIR="${D}" install-world
+       emake DESTDIR="${D}" install
+       emake DESTDIR="${D}" install -C contrib
 
        dodoc README HISTORY doc/{TODO,bug.template}
 
+       # man pages are already built, but if we have the target make them,
+       # they'll be generated from source before being installed so we
+       # manually install man pages.
+       # We use ${SLOT} instead of doman for postgresql.eselect
+       insinto /usr/share/postgresql-${SLOT}/man/
+       doins -r doc/src/sgml/man{1,3,7}
+       if ! use server; then
+               # Remove man pages for non-existent binaries
+               for m in 
{initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
+                       rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
+               done
+       fi
+       docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
+
        insinto /etc/postgresql-${SLOT}
        newins src/bin/psql/psqlrc.sample psqlrc
 
-       dodir /etc/eselect/postgresql/slots/${SLOT}
-       echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
-               "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
-
        use static-libs || find "${ED}" -name '*.a' -delete
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
+       local f bn
+       for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
+                                       -mindepth 1 -maxdepth 1)
+       do
+               bn=$(basename "${f}")
+               # Temporarily tack on tmp to workaround a file collision
+               # issue. This is only necessary for 9.7 and earlier. 10 never
+               # had this issue.
+               dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
+                         "/usr/bin/${bn}${SLOT/.}tmp"
+       done
+
+       local linkname mansec
+       for mansec in {1,3,7} ; do
+               for f in 
"${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
+                       bn=$(basename "${f}")
+                       linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
+                       dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
+                                 /usr/share/man/man${mansec}/${linkname}
+               done
+       done
+
+       if use doc ; then
+               docinto html
+               dodoc doc/src/sgml/html/*
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.init" | newinitd - ${PN}-${SLOT}
+               docinto sgml
+               dodoc doc/src/sgml/*.{sgml,dsl}
+       fi
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.service" | \
-               systemd_newunit - ${PN}-${SLOT}.service
+       if use server; then
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
 
-       newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - 
${PN}-${SLOT}
 
-       use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.service" | \
+                       systemd_newunit - ${PN}-${SLOT}.service
 
-       if use prefix ; then
-               keepdir /run/postgresql
-               fperms 0775 /run/postgresql
+               newbin "${FILESDIR}"/${PN}-check-db-dir 
${PN}-${SLOT}-check-db-dir
+
+               use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account 
session
+
+               if use prefix ; then
+                       keepdir /run/postgresql
+                       fperms 0775 /run/postgresql
+               fi
+       fi
+}
+
+pkg_preinst() {
+       # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+       # /usr/bin/psql96). They may have been created by the
+       # postgresql.eselect module, but they're handled within this ebuild
+       # now. It's alright if we momentarily delete /usr/bin/psql as it
+       # will be recreated by the eselect module in pkg_ppostinst(). This
+       # is only necessary for 9.7 and earlier. 10 and later were never
+       # handled in this manner.
+       local canonicalise
+       if type -p realpath > /dev/null; then
+               canonicalise=realpath
+       elif type -p readlink > /dev/null; then
+               canonicalise='readlink -f'
+       else
+               # can't die, subshell
+               die "No readlink nor realpath found, cannot canonicalise"
        fi
+
+       local l
+       # First remove any symlinks in /usr/bin that may have been created
+       # by the old eselect
+       for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; 
do
+               if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; 
then
+                       rm "${l}" || ewarn "Couldn't remove ${l}"
+               fi
+       done
+
+       # Then move the symlinks created by the ebuild to their proper place.
+       for l in "${ED}"/usr/bin/*tmp ; do
+               mv "${l}" "${l%tmp}" \
+                       || ewarn "Couldn't rename $(basename ${l}) to 
$(basename ${l%tmp})"
+       done
 }
 
 pkg_postinst() {
        postgresql-config update
 
+       if use alpha && use server ; then
+               ewarn "PostgreSQL 9.5+ no longer has native spinlock support on 
Alpha platforms."
+               ewarn "As a result, performance will be extremely degraded."
+       fi
+
        elog "If you need a global psqlrc-file, you can place it in:"
        elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
-       if [[ -z ${REPLACING_VERSIONS} ]] ; then
+       if use server ; then
                elog
-               elog "It looks like this is your first time installing 
PostgreSQL. Run the"
-               elog "following command in all active shells to pick up changes 
to the default"
+               elog "Gentoo specific documentation:"
+               elog "https://wiki.gentoo.org/wiki/PostgreSQL";
+               elog
+               elog "Official documentation:"
+               elog "http://www.postgresql.org/docs/${SLOT}/static/index.html";
+               elog
+               elog "The default location of the Unix-domain socket is:"
+               elog "    ${EROOT%/}/run/postgresql/"
+               elog
+               elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
+               elog "so that it contains your preferred locale in:"
+               elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
+               elog
+               elog "Then, execute the following command to setup the initial 
database"
                elog "environment:"
-               elog "    source /etc/profile"
+               elog "    emerge --config =${CATEGORY}/${PF}"
        fi
-
-       elog
-       elog "Gentoo specific documentation:"
-       elog "https://wiki.gentoo.org/wiki/PostgreSQL";
-       elog
-       elog "Official documentation:"
-       elog "${EROOT%/}/usr/share/doc/${PF}/html"
-       elog
-       elog "The default location of the Unix-domain socket is:"
-       elog "    ${EROOT%/}/run/postgresql/"
-       elog
-       elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
-       elog "so that it contains your preferred locale, and other options, in:"
-       elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
-       elog
-       elog "Then, execute the following command to setup the initial database"
-       elog "environment:"
-       elog "    emerge --config =${CATEGORY}/${PF}"
 }
 
 pkg_prerm() {
-       if [[ -z ${REPLACED_BY_VERSION} ]] ; then
+       if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
                ewarn "Have you dumped and/or migrated the ${SLOT} database 
cluster?"
                ewarn 
"\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL";
 
@@ -249,6 +344,8 @@ pkg_postrm() {
 }
 
 pkg_config() {
+       use server || die "USE flag 'server' not enabled. Nothing to configure."
+
        [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
                && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
        [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
@@ -297,21 +394,10 @@ pkg_config() {
        einfo "The database cluster will be created in:"
        einfo "    ${DATA_DIR}"
        einfo
-       if [ -z "$PG_AUTOCONFIG" ] ; then
-               while [ "$correct" != "true" ] ; do
-                       einfo "Are you ready to continue? (y/n)"
-                       read answer
-                       if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
-                               correct="true"
-                       elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
-                               die "Aborting initialization."
-                       else
-                               echo "Answer not recognized"
-                       fi
-               done
-       else
-               einfo "PG_AUTOCONFIG set, not prompting"
-       fi
+
+       ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
+       sleep 5
+       eend 0
 
        if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
                eerror "The given directory, '${DATA_DIR}', is not empty."
@@ -339,6 +425,10 @@ pkg_config() {
                ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf 
"${DATA_DIR%/}"
        fi
 
+       # unix_socket_directory has no effect in postgresql.conf as it's
+       # overridden in the initscript
+       sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
+
        cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
                # This is here because of 
https://bugs.gentoo.org/show_bug.cgi?id=518522
                # On the off-chance that you might need to work with UTF-8 
encoded
@@ -371,17 +461,17 @@ pkg_config() {
 }
 
 src_test() {
-       einfo ">>> Test phase [check]: ${CATEGORY}/${PF}"
-
-       if [[ ${UID} -ne 0 ]] ; then
+       if use server && [[ ${UID} -ne 0 ]] ; then
                emake check
 
                einfo "If you think other tests besides the regression tests 
are necessary, please"
                einfo "submit a bug including a patch for this ebuild to enable 
them."
        else
+               use server || \
+                       ewarn 'Tests cannot be run without the "server" use 
flag enabled.'
                [[ ${UID} -eq 0 ]] || \
-                       ewarn "Tests cannot be run as root. Enable 'userpriv' 
in FEATURES."
+                       ewarn 'Tests cannot be run as root. Enable "userpriv" 
in FEATURES.'
 
-               ewarn "Skipping."
+               ewarn 'Skipping.'
        fi
 }

diff --git a/dev-db/postgresql/postgresql-9999.ebuild 
b/dev-db/postgresql/postgresql-9.6.2-r1.ebuild
similarity index 55%
copy from dev-db/postgresql/postgresql-9999.ebuild
copy to dev-db/postgresql/postgresql-9.6.2-r1.ebuild
index 6a995c154ba..07527caf315 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9.6.2-r1.ebuild
@@ -5,15 +5,17 @@ EAPI="5"
 
 PYTHON_COMPAT=( python2_7 python3_{4,5,6} )
 
-inherit eutils flag-o-matic git-2 linux-info multilib pam prefix \
-               python-single-r1 systemd user versionator
+inherit eutils flag-o-matic linux-info multilib pam prefix python-single-r1 \
+               systemd user versionator
 
-KEYWORDS=""
+KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~s390 ~sh 
~sparc ~x86 ~amd64-fbsd ~sparc-fbsd ~x86-fbsd ~ppc-macos ~x86-solaris"
 
-# Fix if needed
-SLOT="9.7"
+SLOT="$(get_version_component_range 1-2)"
 
-EGIT_REPO_URI="git://git.postgresql.org/git/postgresql.git"
+MY_PV=${PV/_/}
+S="${WORKDIR}/${PN}-${MY_PV}"
+
+SRC_URI="mirror://postgresql/source/v${MY_PV}/postgresql-${MY_PV}.tar.bz2"
 
 LICENSE="POSTGRESQL GPL-2"
 DESCRIPTION="PostgreSQL RDBMS"
@@ -21,7 +23,7 @@ HOMEPAGE="http://www.postgresql.org/";
 
 LINGUAS="af cs de en es fa fr hr hu it ko nb pl pt_BR ro ru sk sl sv tr
                 zh_CN zh_TW"
-IUSE="kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
+IUSE="doc kerberos kernel_linux ldap libressl nls pam perl -pg_legacytimestamp 
python
          +readline selinux +server ssl static-libs tcl threads uuid xml zlib"
 
 for lingua in ${LINGUAS}; do
@@ -39,7 +41,7 @@ wanted_languages() {
 }
 
 CDEPEND="
->=app-eselect/eselect-postgresql-1.2.0
+>=app-eselect/eselect-postgresql-2.0
 sys-apps/less
 virtual/libintl
 kerberos? ( virtual/krb5 )
@@ -53,30 +55,40 @@ ssl? (
        libressl? ( dev-libs/libressl:= )
 )
 tcl? ( >=dev-lang/tcl-8:0= )
-uuid? ( dev-libs/ossp-uuid )
 xml? ( dev-libs/libxml2 dev-libs/libxslt )
 zlib? ( sys-libs/zlib )
 "
 
+# uuid flags -- depend on sys-apps/util-linux for Linux libcs, or if no
+# supported libc in use depend on dev-libs/ossp-uuid. For BSD systems,
+# the libc includes UUID functions.
+UTIL_LINUX_LIBC=( elibc_{glibc,uclibc,musl} )
+BSD_LIBC=( elibc_{Free,Net,Open}BSD )
+
+nest_usedep() {
+       local front back
+       while [[ ${#} -gt 1 ]]; do
+               front+="${1}? ( "
+               back+=" )"
+               shift
+       done
+       echo "${front}${1}${back}"
+}
+
+IUSE+=" ${UTIL_LINUX_LIBC[@]} ${BSD_LIBC[@]}"
+CDEPEND+="
+uuid? (
+       ${UTIL_LINUX_LIBC[@]/%/? ( sys-apps/util-linux )}
+       $(nest_usedep ${UTIL_LINUX_LIBC[@]/#/!} ${BSD_LIBC[@]/#/!} 
dev-libs/ossp-uuid)
+)"
+
 DEPEND="${CDEPEND}
 !!<sys-apps/sandbox-2.0
->=dev-lang/perl-5.8
-app-text/docbook-dsssl-stylesheets
-app-text/docbook-sgml-dtd:4.2
-app-text/docbook-xml-dtd:4.2
-app-text/docbook-xsl-stylesheets
-app-text/openjade
-dev-libs/libxml2
-dev-libs/libxslt
 sys-devel/bison
 sys-devel/flex
 nls? ( sys-devel/gettext )
 xml? ( virtual/pkgconfig )
 "
-src_unpack() {
-       base_src_unpack
-       git-2_src_unpack
-}
 
 RDEPEND="${CDEPEND}
 !dev-db/postgresql-docs:${SLOT}
@@ -85,17 +97,8 @@ RDEPEND="${CDEPEND}
 selinux? ( sec-policy/selinux-postgresql )
 "
 
-pkg_pretend() {
-       ewarn "You are using a live ebuild that uses the current source code as 
it is"
-       ewarn "available from PostgreSQL's Git repository at emerge time. Given 
such,"
-       ewarn "the GNU Makefiles may be altered by upstream without notice and 
the"
-       ewarn "documentation for this live version is not readily available"
-       ewarn "online. Ergo, the ebuild maintainers will not support building a"
-       ewarn "client-only and/or document-free version."
-}
-
 pkg_setup() {
-       CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
+       use server && CONFIG_CHECK="~SYSVIPC" linux-info_pkg_setup
 
        enewgroup postgres 70
        enewuser postgres 70 /bin/sh /var/lib/postgresql postgres
@@ -116,6 +119,12 @@ src_prepare() {
        # hardened and non-hardened environments. (Bug #528786)
        sed 's/@install_bin@/install -c/' -i src/Makefile.global.in || die
 
+       use server || epatch "${FILESDIR}/${PN}-${SLOT}.1-no-server.patch"
+
+       # Fix bug 486556 where the server would crash at start up because of
+       # an infinite loop caused by a self-referencing symlink.
+       epatch "${FILESDIR}/postgresql-9.2-9.4-tz-dir-overflow.patch"
+
        if use pam ; then
                sed -e "s/\(#define PGSQL_PAM_SERVICE 
\"postgresql\)/\1-${SLOT}/" \
                        -i src/backend/libpq/auth.c || \
@@ -137,6 +146,17 @@ src_configure() {
 
        local PO="${EPREFIX%/}"
 
+       local i uuid_config=""
+       if use uuid; then
+               for i in ${UTIL_LINUX_LIBC[@]}; do
+                       use ${i} && uuid_config="--with-uuid=e2fs"
+               done
+               for i in ${BSD_LIBC[@]}; do
+                       use ${i} && uuid_config="--with-uuid=bsd"
+               done
+               [[ -z $uuid_config ]] && uuid_config="--with-uuid=ossp"
+       fi
+
        econf \
                --prefix="${PO}/usr/$(get_libdir)/postgresql-${SLOT}" \
                --datadir="${PO}/usr/share/postgresql-${SLOT}" \
@@ -155,7 +175,7 @@ src_configure() {
                $(use_with readline) \
                $(use_with ssl openssl) \
                $(use_with tcl) \
-               $(use_with uuid ossp-uuid) \
+               ${uuid_config} \
                $(use_with xml libxml) \
                $(use_with xml libxslt) \
                $(use_with zlib) \
@@ -163,41 +183,119 @@ src_configure() {
 }
 
 src_compile() {
-       emake world
+       emake
+       emake -C contrib
 }
 
 src_install() {
-       emake DESTDIR="${D}" install-world
+       emake DESTDIR="${D}" install
+       emake DESTDIR="${D}" install -C contrib
 
        dodoc README HISTORY doc/{TODO,bug.template}
 
+       # man pages are already built, but if we have the target make them,
+       # they'll be generated from source before being installed so we
+       # manually install man pages.
+       # We use ${SLOT} instead of doman for postgresql.eselect
+       insinto /usr/share/postgresql-${SLOT}/man/
+       doins -r doc/src/sgml/man{1,3,7}
+       if ! use server; then
+               # Remove man pages for non-existent binaries
+               for m in 
{initdb,pg_{controldata,ctl,resetxlog},post{gres,master}}; do
+                       rm "${ED}/usr/share/postgresql-${SLOT}/man/man1/${m}.1"
+               done
+       fi
+       docompress /usr/share/postgresql-${SLOT}/man/man{1,3,7}
+
        insinto /etc/postgresql-${SLOT}
        newins src/bin/psql/psqlrc.sample psqlrc
 
-       dodir /etc/eselect/postgresql/slots/${SLOT}
-       echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
-               "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
-
        use static-libs || find "${ED}" -name '*.a' -delete
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
+       local f bn
+       for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
+                                       -mindepth 1 -maxdepth 1)
+       do
+               bn=$(basename "${f}")
+               # Temporarily tack on tmp to workaround a file collision
+               # issue. This is only necessary for 9.7 and earlier. 10 never
+               # had this issue.
+               dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
+                         "/usr/bin/${bn}${SLOT/.}tmp"
+       done
+
+       local linkname mansec
+       for mansec in {1,3,7} ; do
+               for f in 
"${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
+                       bn=$(basename "${f}")
+                       linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
+                       dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
+                                 /usr/share/man/man${mansec}/${linkname}
+               done
+       done
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.init" | newinitd - ${PN}-${SLOT}
+       if use doc ; then
+               docinto html
+               dodoc doc/src/sgml/html/*
 
-       sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.service" | \
-               systemd_newunit - ${PN}-${SLOT}.service
+               docinto sgml
+               dodoc doc/src/sgml/*.{sgml,dsl}
+       fi
 
-       newbin "${FILESDIR}"/${PN}-check-db-dir ${PN}-${SLOT}-check-db-dir
+       if use server; then
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
 
-       use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - 
${PN}-${SLOT}
 
-       if use prefix ; then
-               keepdir /run/postgresql
-               fperms 0775 /run/postgresql
+               sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
+                       "${FILESDIR}/${PN}.service" | \
+                       systemd_newunit - ${PN}-${SLOT}.service
+
+               newbin "${FILESDIR}"/${PN}-check-db-dir 
${PN}-${SLOT}-check-db-dir
+
+               use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account 
session
+
+               if use prefix ; then
+                       keepdir /run/postgresql
+                       fperms 0775 /run/postgresql
+               fi
+       fi
+}
+
+pkg_preinst() {
+       # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+       # /usr/bin/psql96). They may have been created by the
+       # postgresql.eselect module, but they're handled within this ebuild
+       # now. It's alright if we momentarily delete /usr/bin/psql as it
+       # will be recreated by the eselect module in pkg_ppostinst(). This
+       # is only necessary for 9.7 and earlier. 10 and later were never
+       # handled in this manner.
+       local canonicalise
+       if type -p realpath > /dev/null; then
+               canonicalise=realpath
+       elif type -p readlink > /dev/null; then
+               canonicalise='readlink -f'
+       else
+               # can't die, subshell
+               die "No readlink nor realpath found, cannot canonicalise"
        fi
+
+       local l
+       # First remove any symlinks in /usr/bin that may have been created
+       # by the old eselect
+       for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; 
do
+               if [[ $(${canonicalise} "${l}") == *postgresql-${SLOT}* ]] ; 
then
+                       rm "${l}" || ewarn "Couldn't remove ${l}"
+               fi
+       done
+
+       # Then move the symlinks created by the ebuild to their proper place.
+       for l in "${ED}"/usr/bin/*tmp ; do
+               mv "${l}" "${l%tmp}" \
+                       || ewarn "Couldn't rename $(basename ${l}) to 
$(basename ${l%tmp})"
+       done
 }
 
 pkg_postinst() {
@@ -206,35 +304,29 @@ pkg_postinst() {
        elog "If you need a global psqlrc-file, you can place it in:"
        elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
-       if [[ -z ${REPLACING_VERSIONS} ]] ; then
+       if use server ; then
+               elog
+               elog "Gentoo specific documentation:"
+               elog "https://wiki.gentoo.org/wiki/PostgreSQL";
                elog
-               elog "It looks like this is your first time installing 
PostgreSQL. Run the"
-               elog "following command in all active shells to pick up changes 
to the default"
+               elog "Official documentation:"
+               elog "http://www.postgresql.org/docs/${SLOT}/static/index.html";
+               elog
+               elog "The default location of the Unix-domain socket is:"
+               elog "    ${EROOT%/}/run/postgresql/"
+               elog
+               elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
+               elog "so that it contains your preferred locale in:"
+               elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
+               elog
+               elog "Then, execute the following command to setup the initial 
database"
                elog "environment:"
-               elog "    source /etc/profile"
+               elog "    emerge --config =${CATEGORY}/${PF}"
        fi
-
-       elog
-       elog "Gentoo specific documentation:"
-       elog "https://wiki.gentoo.org/wiki/PostgreSQL";
-       elog
-       elog "Official documentation:"
-       elog "${EROOT%/}/usr/share/doc/${PF}/html"
-       elog
-       elog "The default location of the Unix-domain socket is:"
-       elog "    ${EROOT%/}/run/postgresql/"
-       elog
-       elog "Before initializing the database, you may want to edit 
PG_INITDB_OPTS"
-       elog "so that it contains your preferred locale, and other options, in:"
-       elog "    ${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
-       elog
-       elog "Then, execute the following command to setup the initial database"
-       elog "environment:"
-       elog "    emerge --config =${CATEGORY}/${PF}"
 }
 
 pkg_prerm() {
-       if [[ -z ${REPLACED_BY_VERSION} ]] ; then
+       if use server && [[ -z ${REPLACED_BY_VERSION} ]] ; then
                ewarn "Have you dumped and/or migrated the ${SLOT} database 
cluster?"
                ewarn 
"\thttps://wiki.gentoo.org/wiki/PostgreSQL/QuickStart#Migrating_PostgreSQL";
 
@@ -249,6 +341,8 @@ pkg_postrm() {
 }
 
 pkg_config() {
+       use server || die "USE flag 'server' not enabled. Nothing to configure."
+
        [[ -f "${EROOT%/}/etc/conf.d/postgresql-${SLOT}" ]] \
                && source "${EROOT%/}/etc/conf.d/postgresql-${SLOT}"
        [[ -z "${PGDATA}" ]] && PGDATA="${EROOT%/}/etc/postgresql-${SLOT}/"
@@ -297,21 +391,10 @@ pkg_config() {
        einfo "The database cluster will be created in:"
        einfo "    ${DATA_DIR}"
        einfo
-       if [ -z "$PG_AUTOCONFIG" ] ; then
-               while [ "$correct" != "true" ] ; do
-                       einfo "Are you ready to continue? (y/n)"
-                       read answer
-                       if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
-                               correct="true"
-                       elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
-                               die "Aborting initialization."
-                       else
-                               echo "Answer not recognized"
-                       fi
-               done
-       else
-               einfo "PG_AUTOCONFIG set, not prompting"
-       fi
+
+       ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
+       sleep 5
+       eend 0
 
        if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
                eerror "The given directory, '${DATA_DIR}', is not empty."
@@ -339,6 +422,10 @@ pkg_config() {
                ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf 
"${DATA_DIR%/}"
        fi
 
+       # unix_socket_directory has no effect in postgresql.conf as it's
+       # overridden in the initscript
+       sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
+
        cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
                # This is here because of 
https://bugs.gentoo.org/show_bug.cgi?id=518522
                # On the off-chance that you might need to work with UTF-8 
encoded
@@ -371,17 +458,17 @@ pkg_config() {
 }
 
 src_test() {
-       einfo ">>> Test phase [check]: ${CATEGORY}/${PF}"
-
-       if [[ ${UID} -ne 0 ]] ; then
+       if use server && [[ ${UID} -ne 0 ]] ; then
                emake check
 
                einfo "If you think other tests besides the regression tests 
are necessary, please"
                einfo "submit a bug including a patch for this ebuild to enable 
them."
        else
+               use server || \
+                       ewarn 'Tests cannot be run without the "server" use 
flag enabled.'
                [[ ${UID} -eq 0 ]] || \
-                       ewarn "Tests cannot be run as root. Enable 'userpriv' 
in FEATURES."
+                       ewarn 'Tests cannot be run as root. Enable "userpriv" 
in FEATURES.'
 
-               ewarn "Skipping."
+               ewarn 'Skipping.'
        fi
 }

diff --git a/dev-db/postgresql/postgresql-9999.ebuild 
b/dev-db/postgresql/postgresql-9999.ebuild
index 6a995c154ba..4ef8157f431 100644
--- a/dev-db/postgresql/postgresql-9999.ebuild
+++ b/dev-db/postgresql/postgresql-9999.ebuild
@@ -10,8 +10,8 @@ inherit eutils flag-o-matic git-2 linux-info multilib pam 
prefix \
 
 KEYWORDS=""
 
-# Fix if needed
-SLOT="9.7"
+# Bump when rc released.
+SLOT="10"
 
 EGIT_REPO_URI="git://git.postgresql.org/git/postgresql.git"
 
@@ -39,7 +39,7 @@ wanted_languages() {
 }
 
 CDEPEND="
->=app-eselect/eselect-postgresql-1.2.0
+>=app-eselect/eselect-postgresql-2.0
 sys-apps/less
 virtual/libintl
 kerberos? ( virtual/krb5 )
@@ -174,17 +174,13 @@ src_install() {
        insinto /etc/postgresql-${SLOT}
        newins src/bin/psql/psqlrc.sample psqlrc
 
-       dodir /etc/eselect/postgresql/slots/${SLOT}
-       echo "postgres_ebuilds=\"\${postgres_ebuilds} ${PF}\"" > \
-               "${ED}/etc/eselect/postgresql/slots/${SLOT}/base"
-
        use static-libs || find "${ED}" -name '*.a' -delete
 
        sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.confd" | newconfd - ${PN}-${SLOT}
+               "${FILESDIR}/${PN}.confd-9.3" | newconfd - ${PN}-${SLOT}
 
        sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
-               "${FILESDIR}/${PN}.init" | newinitd - ${PN}-${SLOT}
+               "${FILESDIR}/${PN}.init-9.3-r1" | newinitd - ${PN}-${SLOT}
 
        sed -e "s|@SLOT@|${SLOT}|g" -e "s|@LIBDIR@|$(get_libdir)|g" \
                "${FILESDIR}/${PN}.service" | \
@@ -194,26 +190,74 @@ src_install() {
 
        use pam && pamd_mimic system-auth ${PN}-${SLOT} auth account session
 
+       local f bn
+       for f in $(find "${ED}/usr/$(get_libdir)/postgresql-${SLOT}/bin" \
+                                       -mindepth 1 -maxdepth 1)
+       do
+               bn=$(basename "${f}")
+               # Temporarily tack on tmp to workaround a file collision
+               # issue. This is only necessary for 9.7 and earlier. 10 never
+               # had this issue.
+               dosym "../$(get_libdir)/postgresql-${SLOT}/bin/${bn}" \
+                         "/usr/bin/${bn}${SLOT/.}tmp"
+       done
+
+       local linkname mansec
+       for mansec in {1,3,7} ; do
+               for f in 
"${ED}"/usr/share/postgresql-${SLOT}/man/man${mansec}/* ; do
+                       bn=$(basename "${f}")
+                       linkname=${bn/%.${mansec}/${SLOT/.}.${mansec}}
+                       dosym ../../postgresql-${SLOT}/man/man${mansec}/$bn \
+                                 /usr/share/man/man${mansec}/${linkname}
+               done
+       done
+
        if use prefix ; then
                keepdir /run/postgresql
                fperms 0775 /run/postgresql
        fi
 }
 
+pkg_preinst() {
+       # Find all of the slot-specific symlinks, if any, in /usr/bin (e.g.,
+       # /usr/bin/psql97). They may have been created by the
+       # postgresql.eselect module, but they're handled within this ebuild
+       # now. It's alright if we momentarily delete /usr/bin/psql as it
+       # will be recreated by the eselect module in pkg_ppostinst().  We
+       # only worry about the 9.7 slot as that's the last slot that had its
+       # slot-specific links generated by eselect.
+       #
+       # This can be removed when 10 is the lowest slot in the tree.
+       local canonicalise
+       if type -p realpath > /dev/null; then
+               canonicalise=realpath
+       elif type -p readlink > /dev/null; then
+               canonicalise='readlink -f'
+       else
+               # can't die, subshell
+               die "No readlink nor realpath found, cannot canonicalise"
+       fi
+
+       local l
+       # First remove any symlinks in /usr/bin that may have been created
+       # by the old eselect
+       for l in $(find "${ROOT%/}/usr/bin" -mindepth 1 -maxdepth 1 -type l) ; 
do
+               [[ $(${canonicalise} "${l}") == *postgresql-9.7* ]] && rm "${l}"
+       done
+
+       # Then move the symlinks created by the ebuild to their proper place.
+       for l in "${ED}"/usr/bin/*tmp ; do
+               mv "${l}" "${l%tmp}" \
+                       || ewarn "Couldn't rename $(basename ${l}) to 
$(basename ${l%tmp})"
+       done
+}
+
 pkg_postinst() {
        postgresql-config update
 
        elog "If you need a global psqlrc-file, you can place it in:"
        elog "    ${EROOT%/}/etc/postgresql-${SLOT}/"
 
-       if [[ -z ${REPLACING_VERSIONS} ]] ; then
-               elog
-               elog "It looks like this is your first time installing 
PostgreSQL. Run the"
-               elog "following command in all active shells to pick up changes 
to the default"
-               elog "environment:"
-               elog "    source /etc/profile"
-       fi
-
        elog
        elog "Gentoo specific documentation:"
        elog "https://wiki.gentoo.org/wiki/PostgreSQL";
@@ -297,21 +341,10 @@ pkg_config() {
        einfo "The database cluster will be created in:"
        einfo "    ${DATA_DIR}"
        einfo
-       if [ -z "$PG_AUTOCONFIG" ] ; then
-               while [ "$correct" != "true" ] ; do
-                       einfo "Are you ready to continue? (y/n)"
-                       read answer
-                       if [[ $answer =~ ^[Yy]([Ee][Ss])?$ ]] ; then
-                               correct="true"
-                       elif [[ $answer =~ ^[Nn]([Oo])?$ ]] ; then
-                               die "Aborting initialization."
-                       else
-                               echo "Answer not recognized"
-                       fi
-               done
-       else
-               einfo "PG_AUTOCONFIG set, not prompting"
-       fi
+
+       ebegin "Continuing initialization in 5 seconds (Control-C to cancel)"
+       sleep 5
+       eend 0
 
        if [ -n "$(ls -A ${DATA_DIR} 2> /dev/null)" ] ; then
                eerror "The given directory, '${DATA_DIR}', is not empty."
@@ -339,6 +372,10 @@ pkg_config() {
                ln -s "${PGDATA%/}"/{pg_{hba,ident},postgresql}.conf 
"${DATA_DIR%/}"
        fi
 
+       # unix_socket_directory has no effect in postgresql.conf as it's
+       # overridden in the initscript
+       sed '/^#unix_socket_directories/,+1d' -i "${PGDATA%/}"/postgresql.conf
+
        cat <<- EOF >> "${PGDATA%/}"/postgresql.conf
                # This is here because of 
https://bugs.gentoo.org/show_bug.cgi?id=518522
                # On the off-chance that you might need to work with UTF-8 
encoded

Reply via email to