File: pg_wrapper

package info (click to toggle)
postgresql-common 94lenny1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 624 kB
  • ctags: 97
  • sloc: perl: 2,268; sh: 220; makefile: 14
file content (182 lines) | stat: -rwxr-xr-x 4,787 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl -w
# Call a PostgreSQL client program with the version, cluster and default
# database specified in ~/.postgresqlrc or
# /etc/postgresql-common/user_clusters.
#
# (C) 2005 Martin Pitt <mpitt@debian.org>

use strict;

use lib '/usr/share/postgresql-common';
use PgCommon;

my ($version, $cluster, $db, $port, $host);

$host = $ENV{'PGHOST'};

# Check for PGCLUSTER in %ENV
if (defined $ENV{'PGCLUSTER'}) {
    ($version, $cluster) = split ('/', $ENV{'PGCLUSTER'}, 2);
    error 'Invalid version specified with $PGCLUSTER' unless version_exists $version;
    error 'No cluster specified with $PGCLUSTER' unless $cluster;
}

# Check for --cluster argument and filter it out
for (my $i = 0; $i <= $#ARGV; ++$i) {
    if ($ARGV[$i] eq '--cluster') {
        error '--cluster option needs an argument (<version>/<cluster>)' if ($i >= $#ARGV);

        ($version, $cluster) = split ('/', $ARGV[$i+1], 2);
	$host = undef; # --cluster overrides $PGHOST env var
        error 'Invalid version specified with --cluster' unless version_exists $version;
        error 'No cluster specified with --cluster' unless $cluster;

        splice @ARGV, $i, 2;
        last;
    }
}

# Determine $version, $cluster, $db, $port from map files
($version, $cluster, $db) = user_cluster_map() unless $cluster;

# check if we have a network cluster
if (!$host && $cluster && !cluster_exists $version, $cluster) {
    if ($cluster =~ /^(\S+):(\d*)$/) {
	$host = $1;
	$port = $2 || 5432;
    } else {
	error 'Specified cluster does not exist locally and does not specify a remote cluster';
    }
}

if (!$host && $cluster) {
    $port = get_cluster_port($version, $cluster);

    unless ($ENV{'PGHOST'}) {
        # default to cluster specific Unix socket directory
        $ENV{'PGHOST'} = get_cluster_socketdir $version, $cluster;
    }
}

$ENV{'PGSYSCONFDIR'} = '/etc/postgresql-common' if !$ENV{'PGSYSCONFDIR'};
$ENV{'PGPORT'} = $port if $port && !$ENV{'PGPORT'};
$ENV{'PGDATABASE'} = $db if $db && !$ENV{'PGDATABASE'};
$ENV{'PGHOST'} = $host if $host;

unless ($version) {
    if (get_versions) {
	error 'No existing local cluster is suitable as a default target. Please see man pg_wrapper(1) how to specify one.';
    } else {
	error 'You must install at least one postgresql-client-<version> package.';
    }
}

error 'Invalid PostgreSQL cluster version' unless -d "/usr/lib/postgresql/$version";
my $cmd = get_program_path (((split '/', $0)[-1]), $version);
error 'pg_wrapper: invalid command name' unless $cmd;
unshift @ARGV, $cmd;
exec @ARGV;

__END__

=head1 NAME

pg_wrapper - wrapper for PostgreSQL client commands

=head1 SYNOPSIS

I<client-program> [B<--cluster> I<version>/I<cluster>] [...]

(I<client-program>: B<psql>, B<createdb>, B<dropuser>, and all other client
programs installed in C</usr/lib/postgresql/>I<version>C</bin>).

=head1 DESCRIPTION

This program is run only as a link to names which correspond to PostgreSQL
programs in C</usr/lib/postgresql/>I<version>C</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
specifed options to that command.

The target cluster is selected by the following means, in descending order of
precedence:

=over

=item 1.

explicit specification with the B<--cluster> option

=item 2.

explicit specification with the B<PGCLUSTER> environment variable

=item 3.

matching entry in C<~/.postgresqlrc> (see L<postgresqlrc(5)>), if that
file exists

=item 4.

matching entry in C</etc/postgresql-common/user_clusters> (see
L<user_clusters(5)>), if that file exists

=item 5.

If only one local cluster exists, that one will be selected.

=item 6.

If several local clusters exist, the one listening on the default port 5432
will be selected.

=back

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

=head1 OPTIONS

=over

=item B<--cluster> I<version>B</>I<cluster>

I<cluster> is either the name of a local cluster, or takes the form
I<host>:I<port> for a remote cluster. If I<port> is left empty (i. e. you just
specify I<host:>), it defaults to 5432.

=back

=head1 ENVIRONMENT

=over

=item B<PGCLUSTER>

If C<$PGCLUSTER> is set, its value (of the form I<version>/I<cluster>)
specifies the desired cluster, similar to the B<--cluster> option. However, if
B<--cluster> is specified, it overrides the value of C<$PGCLUSTER>.

=back

=head1 FILES

=over

=item C</etc/postgresql-common/user_clusters>

stores the default cluster and database for users and groups as set by
the administrators. 

=item C<$HOME/.postgresqlrc>

stores defaults set by the user himself.

=back

=head1 SEE ALSO

L<user_clusters(5)>, L<postgresqlrc(5)>

=head1 AUTHOR

Martin Pitt L<E<lt>mpitt@debian.orgE<gt>>