I have Postgresql versions 8.4 and 9.1 installed. For any given Postgresql command, how do I specify a specific version of the command to run? (e.g., psql, pg_dump, pg_ctlcluster, pg_restore, ...)

My question is motivated by wanting to do a pg_dump in preparation for an upgrade from 8.4 to 9.1, and I want to know which pg_dump version I am running.

I'm running on Ubuntu 10.04 Natty.


4 Answers 4


You are on Ubuntu and obviously have Martin Pitt's pg_wrapper installed (judging from pg_ctlcluster) that is provided by the package postgresql-common and comes with the standard Debian packages. I use the same on Debian.

On a Linux system, run which in the shell to see which executable is actually picked:

postgres@db:~$ which pg_dump
postgres@db:~$ ls -l /usr/bin/pg_dump
lrwxrwxrwx 1 root root 37  4. Jun 18:57 /usr/bin/pg_dump -> ../share/postgresql-common/pg_wrapper

pg_dump is actually a symlink to pg_wrapper, which dynamically picks the appropriate version of the client program for the db cluster you run pg_dump with. I quote the man page of pg_wrapper:

This program is run only as a link to names which correspond to PostgreSQL programs in /usr/lib/postgresql/version/bin. It determines the configured cluster and database for the user and calls the appropriate version of the desired program to connect to that cluster and database, supplying any specified options to that command.

   The target cluster is selected by the following means, in descending order of precedence:
   1.  explicit specification with the --cluster option
   2.  explicit specification with the PGCLUSTER environment variable
   3.  matching entry in ~/.postgresqlrc (see postgresqlrc(5)), if that file exists
   4.  matching entry in /etc/postgresql-common/user_clusters (see user_clusters(5)), if that file exists
   5.  If only one local cluster exists, that one will be selected.
   6.  If several local clusters exist, the one listening on the default port 5432 will be selected.

   If none of these rules match, pg_wrapper aborts with an error.

IOW, the right version should be picked automagically - unless you screwed up your installation somehow. You can always add the option --cluster to be specific.

  • 2
    Just a note: this doesn't help with remote connections. I have 8.3 and 9.1 client tools installed and it defaults to 8.3 for some reason. I have to manually select the path in /usr/lib to use the correct version. I would've thought that the newest one would be used by default, but seems not. Commented Mar 28, 2014 at 10:59
  • You do not need the newest one, but the one matching your database. For some purposes the new version of pg_dump may be the better option, though. Commented Mar 28, 2014 at 15:22
  • Can you please clarify what exactly needs to go in the ~/.postgreslqrc file? My client output --> psql (8.4.21, server 9.1.13) WARNING: psql version 8.4, server version 9.1. Some psql features might not work. Commented May 14, 2014 at 6:36
  • That does not seem to work for me. I created a dump on a server with 9.1, and when I tried to restore on another server with 9.1 and 8.4, it tells me it cannot recognize the file format. Also, there is no --cluster command line option. Commented Jun 10, 2014 at 5:31

I use

PGCLUSTER=8.4/main pg_dump ...
PGCLUSTER=9.1/main pg_dump ...

previous solutions does not worker for me on Debian buster and uppper. I ended up creating by own scripts which should replace binaries installed into /usr/bin/ from package.

here's script for pg_dump


echoerr() { echo "$@" 1>&2; }


if [ -f "$pg_version_file" ]; then
        # echo "pg_version file found $pg_version_file with version $(cat $pg_version_file)"
        default_version=$(cat $pg_version_file)
# else
        # echo "pg_version file not found. default version is $default_version"

valid_versions="9.3 9.4 9.5 9.6 10 11 12 13"

if [[ ! " $valid_versions " =~ .*\ $version\ .* ]]
        echoerr "Invalid version $version"
        echoerr "Should be one of $valid_versions"
        exit -1


if [ ! -f "$pg_dump" ]; then
        echoerr "File $pg_dump not found!"
        exit -1

# echo "use version $version"

"$pg_dump" "$@"

I hope it will be useful for someone. Just change pg_dump to psql or any other version specific pg binary and you will get script for that binary.

Keep note that you should apply them every time postgresql-client-common package is upgrading

here's how I apply them


mv -v /usr/bin/pg_dump /usr/bin/pg_dump.orig &&
mv -v /usr/bin/psql /usr/bin/psql.orig &&
cp -v /scripts/pg_dump.sh /usr/bin/pg_dump &&
cp -v /scripts/psql.sh /usr/bin/psql &&
echo 'Setup completed.'

In Ubuntu 20.04:

export PGCLUSTER=9.6/main    

pg_dump -V    

pg_dump (PostgreSQL) 9.6.21    
  • 1
    can you elaborate your answerß
    – nbk
    Commented Mar 22, 2021 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.