pg_config shows 9.4 instead of 9.3

This got fixed for me by running:

sudo apt-get install postgresql-server-dev-9.3

I still don't really know how I got into that state though.


I got into the same problem by running :

apt-get install postgresql-server-dev-all

on a pg9.4 instance, which installed the package for versions 9.1, 9.2, 9.3, 9.4 and 9.5.

It turns out that this script :

/usr/bin/pg_config

finds the latest version and runs it :

#!/bin/sh

# If postgresql-server-dev-* is installed, call pg_config from the latest
# available one. Otherwise fall back to libpq-dev's version.
#
# (C) 2011 Martin Pitt <[email protected]>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
set -e
PGBINROOT="/usr/lib/postgresql/"
LATEST_SERVER_DEV=\`ls $PGBINROOT*/bin/pg_config 2>/dev/null|tail -n1\`

if [ -n "$LATEST_SERVER_DEV" ]; then
    exec "$LATEST_SERVER_DEV" "$@"
else
    if [ -x /usr/bin/pg_config.libpq-dev ]; then
    exec /usr/bin/pg_config.libpq-dev "$@"
    else
    echo "You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application." >&2
    exit 1
    fi
fi

The solution was to remove the directory : /usr/lib/postgresql/9.5


Ubuntu designs its Postgres packages so that most commands will take a PGCLUSTER envvar with a value like 9.5/main to let you control which version/cluster the command should use. Sadly pg_config doesn't! But /usr/bin/pg_config is just a bash wrapper, so I added a few lines like so:

set -e
PGBINROOT="/usr/lib/postgresql/"
#redhat# PGBINROOT="/usr/pgsql-"

# MY CHANGES START HERE
if [ -n "$PGCLUSTER" ]; then
  exec "$PGBINROOT/$PGCLUSTER/bin/pg_config" "$@"
fi
# MY CHANGES END HERE

LATEST_SERVER_DEV=`ls $PGBINROOT*/bin/pg_config 2>/dev/null|tail -n1`

if [ -n "$LATEST_SERVER_DEV" ]; then
    exec "$LATEST_SERVER_DEV" "$@"
else
    if [ -x /usr/bin/pg_config.libpq-dev ]; then
  exec /usr/bin/pg_config.libpq-dev "$@"
    else
  echo "You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application." >&2
  exit 1
    fi
fi

That doesn't support the cluster part of the usual PGCLUSTER string, but you can still say PGCLUSTER=9.3 make e.g. This helps me when I'm building postgres extensions for a version I have installed that isn't the most recent.

I guess it might be safer to use a different name for my variable here, but it hasn't caused me problems so far. :-)