Upgrading to GitLab 12.1.0.

pg_wrapper 8.74 KB
Newer Older
1
#!/usr/bin/perl
2 3 4 5
# Call a PostgreSQL client program with the version, cluster and default
# database specified in ~/.postgresqlrc or
# /etc/postgresql-common/user_clusters.
#
6
# (C) 2005-2009 Martin Pitt <mpitt@debian.org>
7
# (C) 2013-2017 Christoph Berg <myon@debian.org>
8 9 10 11 12 13 14 15 16 17
#
#  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.
18

19
use warnings;
Martin Pitt's avatar
Martin Pitt committed
20
use strict;
21
use POSIX;
22
use PgCommon;
23

24 25 26 27
my ($version, $cluster);
my $explicit_host = exists $ENV{PGHOST};
my $explicit_port = exists $ENV{PGPORT};
my $explicit_service = exists $ENV{PGSERVICE};
Martin Pitt's avatar
Martin Pitt committed
28

29 30
# Evaluate PGCLUSTER (unless PGHOST is set as well)
if (exists $ENV{'PGCLUSTER'} and not $explicit_host) {
31
    ($version, $cluster) = split ('/', $ENV{'PGCLUSTER'}, 2);
32
    error "Invalid version $version specified in PGCLUSTER" unless version_exists $version;
33
    error 'No cluster specified with $PGCLUSTER' unless $cluster;
34 35
}

36
# Check for --cluster argument and filter it out
Martin Pitt's avatar
Martin Pitt committed
37
for (my $i = 0; $i <= $#ARGV; ++$i) {
38 39
    last if $ARGV[$i] eq '--';

40 41 42 43
    if ($ARGV[$i] eq '--cluster') {
        error '--cluster option needs an argument (<version>/<cluster>)' if ($i >= $#ARGV);

        ($version, $cluster) = split ('/', $ARGV[$i+1], 2);
44
        error "Invalid version $version specified with --cluster" unless version_exists $version;
45
        error 'No cluster specified with --cluster' unless $cluster;
46

47 48
        splice @ARGV, $i, 2;
        last;
49
    } elsif ($ARGV[$i] =~ /^--cluster=(\d+\.?\d)\/(.+)/) {
50
        ($version, $cluster) = ($1, $2);
51
        error "Invalid version $version specified with --cluster" unless version_exists $version;
52 53 54 55
        error 'No cluster specified with --cluster' unless $cluster;

        splice @ARGV, $i, 1;
        last;
56
    }
57 58 59 60 61 62 63 64 65 66 67 68 69 70
    # --host or -h on command line, drop info from PGCLUSTER
    if ($ARGV[$i] =~ /^--host\b/ || $ARGV[$i] =~ /^-\w*h\w*$/) {
        ($version, $cluster) = (undef, undef);
        $explicit_host = 1;
        delete $ENV{PGCLUSTER};
    }
    # --port or -p on command line
    if ($ARGV[$i] =~ /^--port\b/ || $ARGV[$i] =~ /^-\w*p\w*$/) {
        $explicit_port = 1;
    }
    # "service=" in connection string
    if ($ARGV[$i] =~ /\bservice=/) {
        $explicit_service = 1;
    }
71 72
}

73 74 75 76 77
my ($db);

# if we don't have a cluster, and no specific host was given, consult postgresqlrc
# or fall back to default port cluster (on 5432), or undef otherwise
($version, $cluster, $db) = user_cluster_map() unless ($cluster or $explicit_host);
78

79 80 81 82
my ($host, $port);

if ($cluster) {
    # check if we have a network cluster (N.N/the.host.name:port)
83 84
    if ($cluster =~ /^(\S+):(\d*)$/) {
	$host = $1;
85
	$port = $2 || $PgCommon::defaultport;
86 87 88
    } elsif (not cluster_exists($version, $cluster)) {
        # a specific cluster was requested, error out because it doesn't exist
        error "Cluster $version $cluster does not exist";
89
    } else {
90 91
        $host = get_cluster_socketdir ($version, $cluster);
        $port = get_cluster_port($version, $cluster);
92
    }
93 94
    # set PGCLUSTER variable for information
    $ENV{PGCLUSTER} = "$version/$cluster";
95 96
}

97 98 99
# setup environment
$ENV{'PGSYSCONFDIR'} //= '/etc/postgresql-common';
$ENV{'PGHOST'} = $host if ($host);
100
$ENV{'PGPORT'} = $port if $port && !$ENV{'PGPORT'};
101
$ENV{'PGDATABASE'} = $db if $db && !$ENV{'PGDATABASE'};
102

103 104 105 106 107
# check under which name we were called
my $cmdname = (split '/', $0)[-1];

unless ($version or $explicit_host or $explicit_port or $explicit_service) {
    print STDERR "Warning: No existing local cluster is suitable as a default target. Please see man pg_wrapper(1) how to specify one.\n";
108 109
}

110 111 112 113 114
# if we have no version yet, use the latest version. We were called as psql,
# pg_archivecleanup, or pg_isready, always use latest version
if (not $version or $cmdname =~ /^(psql|pg_archivecleanup|pg_isready)$/) {
    $version = get_newest_version;
}
115
unless ($version) {
116
    error 'You must install at least one postgresql-client-<version> package';
117
}
118
error "PostgreSQL version $version is not installed" unless -d "$PgCommon::binroot$version";
119

120
my $cmd;
121
if ($cmdname eq 'pg_wrapper') {
122 123 124
    error "pg_wrapper called directly but no program given as argument"
        if (@ARGV == 0);
    $cmd = shift; # will be unshifted back below
125 126 127
} else {
    $cmd = get_program_path ($cmdname, $version);
}
128 129

# libreadline is a lot better than libedit, so prefer that
130
if ($cmdname eq 'psql' and not $PgCommon::rpm) {
131
    my @readlines;
132 133 134 135 136 137 138 139 140
    # non-multiarch path
    @readlines = sort(</lib/libreadline.so.?>);

    unless (@readlines) {
	# get multiarch dir for our architecture
	if (open PS, '-|', '/usr/bin/ldd', $cmd) {
	    my $out;
	    read PS, $out, 10000;
	    close PS;
141 142 143 144 145 146 147 148 149 150
	    if ($out =~ m!/libreadline.so!) {
		# already linked against libreadline
		@readlines = ();
	    }
	    else
	    {
		my ($lib_path) = $out =~ m!(/lib/.*)/libedit.so!;

		@readlines = sort(<$lib_path/libreadline.so.?>);
	    }
151 152 153
	}
    }

154 155 156 157 158
    if (@readlines) {
	$ENV{'LD_PRELOAD'} = ($ENV{'LD_PRELOAD'} or '') . ':' . $readlines[-1];
    }
}

159
error "pg_wrapper: $cmdname was not found in $PgCommon::binroot$version/bin" unless $cmd;
160 161
unshift @ARGV, $cmd;
exec @ARGV;
162

Martin Pitt's avatar
Martin Pitt committed
163 164 165 166 167 168
__END__

=head1 NAME

pg_wrapper - wrapper for PostgreSQL client commands

169 170
=head1 SYNOPSIS

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

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

Martin Pitt's avatar
Martin Pitt committed
176 177 178
=head1 DESCRIPTION

This program is run only as a link to names which correspond to PostgreSQL
Martin Pitt's avatar
Martin Pitt committed
179
programs in C</usr/lib/postgresql/>I<version>C</bin>. It determines the
180 181
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
182
specified options to that command.
183

184 185 186 187 188 189 190
The target cluster is selected by the following means, in descending order of
precedence:

=over

=item 1.

191
explicit specification with the B<--host> option
192 193 194

=item 2.

195
explicit specification with the B<--cluster> option
196 197 198

=item 3.

199 200 201
if the B<PGHOST> environment variable is set, no further cluster selection is
performed. The default PostgreSQL version and port number (from the command
line, the environment variable B<PGPORT>, or default 5432) will be used.
202 203 204

=item 4.

205 206 207 208
explicit specification with the B<PGCLUSTER> environment variable

=item 5.

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

212
=item 6.
213 214 215 216

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

217
=item 7.
218 219 220

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

221
=item 8.
222 223 224 225 226 227

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

=back

228 229 230
If none of these rules match, B<pg_wrapper> does not set any environment
variables and the program called will likely error out with a message like
"could not connect to server: Connection refused".
231

232
For B<psql>, B<pg_archivecleanup>, and B<pg_isready>, B<pg_wrapper> will always use the binary from
233 234
the newest PostgreSQL version installed, as these are downwards compatible.

235 236 237 238 239 240
=head1 OPTIONS

=over

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

241 242
=item B<--cluster> I<version>B</>I<host>B<:>[I<port>]

243 244 245 246 247
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
248 249 250 251 252 253 254

=head1 ENVIRONMENT

=over

=item B<PGCLUSTER>

Martin Pitt's avatar
Martin Pitt committed
255
If C<$PGCLUSTER> is set, its value (of the form I<version>/I<cluster>)
256
specifies the desired cluster, similar to the B<--cluster> option. However, if
Martin Pitt's avatar
Martin Pitt committed
257
B<--cluster> is specified, it overrides the value of C<$PGCLUSTER>.
258

259 260 261 262 263 264 265
=item B<PG_CLUSTER_CONF_ROOT>

This specifies an alternative base directory for cluster configurations. This
is usually C</etc/postgresql/>, but for testing/development purposes you can
change this to point to e. g. your home directory, so that you can use the
postgresql-common tools without root privileges.

266 267 268 269 270 271
=item B<PGSYSCONFDIR>

This is the location of PostgreSQL's and postgresql-common's global
configuration (e. g. C<pg_service.conf>, L<user_clusters(5)>). The default is
C</etc/postgresql-common/>.

272
=back
Martin Pitt's avatar
Martin Pitt committed
273 274 275 276 277

=head1 FILES

=over

Martin Pitt's avatar
Martin Pitt committed
278
=item C</etc/postgresql-common/user_clusters>
Martin Pitt's avatar
Martin Pitt committed
279 280 281 282

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

Martin Pitt's avatar
Martin Pitt committed
283
=item C<$HOME/.postgresqlrc>
Martin Pitt's avatar
Martin Pitt committed
284 285 286 287 288 289 290

stores defaults set by the user himself.

=back

=head1 SEE ALSO

Martin Pitt's avatar
Martin Pitt committed
291
L<user_clusters(5)>, L<postgresqlrc(5)>
Martin Pitt's avatar
Martin Pitt committed
292 293 294 295

=head1 AUTHOR

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