HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlModerate

How to run specific version (8.4, 9.1) of postgresql pg_* command (e.g., pg_dump)

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlpg_dumphowversionspecificcommandpg_run

Problem

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.

Solution

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
/usr/bin/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.

Code Snippets

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.

Context

StackExchange Database Administrators Q#21006, answer score: 18

Revisions (0)

No revisions yet.