| 1 |
#!/usr/bin/perl |
| 2 |
# Call a PostgreSQL client program with the version, cluster and default |
| 3 |
# database specified in ~/.postgresqlrc or |
| 4 |
# /etc/postgresql-common/user_clusters. |
| 5 |
# |
| 6 |
# (C) 2005-2009 Martin Pitt <[email protected]> |
| 7 |
# (C) 2013-2022 Christoph Berg <[email protected]> |
| 8 |
# |
| 9 |
# This program is free software; you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU General Public License as published by |
| 11 |
# the Free Software Foundation; either version 2 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# This program is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU General Public License for more details. |
| 18 |
|
| 19 |
use warnings; |
| 20 |
use strict; |
| 21 |
use POSIX; |
| 22 |
use PgCommon; |
| 23 |
|
| 24 |
my ($version, $cluster); |
| 25 |
my $explicit_host = exists $ENV{PGHOST}; |
| 26 |
my $explicit_port = $ENV{PGPORT}; |
| 27 |
my $explicit_service = exists $ENV{PGSERVICE}; |
| 28 |
|
| 29 |
# Evaluate PGCLUSTER (unless PGHOST is set as well) |
| 30 |
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 |
| 37 |
for (my $i = 0; $i <= $#ARGV; ++$i) { |
| 38 |
last if $ARGV[$i] eq '--'; |
| 39 |
|
| 40 |
if ($ARGV[$i] eq '--cluster') { |
| 41 |
error '--cluster option needs an argument (<version>/<cluster>)' if ($i >= $#ARGV); |
| 42 |
|
| 43 |
($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 |
splice @ARGV, $i, 2; |
| 48 |
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 |
error 'No cluster specified with --cluster' unless $cluster; |
| 53 |
|
| 54 |
splice @ARGV, $i, 1; |
| 55 |
last; |
| 56 |
} |
| 57 |
# --host or -h on command line, drop info from PGCLUSTER |
| 58 |
if ($ARGV[$i] =~ /^--host\b/ || $ARGV[$i] =~ /^-\w*h\w*$/) { |
| 59 |
($version, $cluster) = (undef, undef); |
| 60 |
$explicit_host = 1; |
| 61 |
delete $ENV{PGCLUSTER}; |
| 62 |
} |
| 63 |
# --port or -p on command line |
| 64 |
if ($ARGV[$i] =~ /^--port\b(?:=(\d+))?/ || $ARGV[$i] =~ /^-\w*p(\d+)?$/) { |
| 65 |
if (defined $1) { |
| 66 |
$explicit_port = $1; |
| 67 |
} elsif ($i < $#ARGV) { |
| 68 |
$explicit_port = $ARGV[$i+1]; |
| 69 |
} |
| 70 |
} |
| 71 |
# "service=" in connection string |
| 72 |
if ($ARGV[$i] =~ /\bservice=/) { |
| 73 |
$explicit_service = 1; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
# if only a port is specified, look for local cluster on specified port |
| 78 |
if ($explicit_port and not $version and not $cluster and not $explicit_host and not $explicit_service) { |
| 79 |
LOOP: foreach my $v (reverse get_versions()) { |
| 80 |
foreach my $c (get_version_clusters $v) { |
| 81 |
my $p = get_cluster_port $v, $c; |
| 82 |
if ($p eq $explicit_port) { |
| 83 |
$version = $v; |
| 84 |
# set PGCLUSTER variable for information |
| 85 |
$ENV{PGCLUSTER} = "$version/$c"; |
| 86 |
last LOOP; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
# if we don't have a cluster, and no specific host or port was given, consult postgresqlrc |
| 93 |
# or fall back to default port cluster (on 5432), or undef otherwise |
| 94 |
my ($db); |
| 95 |
($version, $cluster, $db) = user_cluster_map() unless ($cluster or $explicit_host or $explicit_port); |
| 96 |
|
| 97 |
my ($host, $port); |
| 98 |
|
| 99 |
if ($cluster) { |
| 100 |
# check if we have a network cluster (N.N/the.host.name:port) |
| 101 |
if ($cluster =~ /^(\S+):(\d*)$/) { |
| 102 |
$host = $1; |
| 103 |
$port = $2 || $PgCommon::defaultport; |
| 104 |
} elsif (not cluster_exists($version, $cluster)) { |
| 105 |
# a specific cluster was requested, error out because it doesn't exist |
| 106 |
error "Cluster $version $cluster does not exist"; |
| 107 |
} else { |
| 108 |
$host = get_cluster_socketdir ($version, $cluster); |
| 109 |
$port = get_cluster_port($version, $cluster); |
| 110 |
} |
| 111 |
# set PGCLUSTER variable for information |
| 112 |
$ENV{PGCLUSTER} = "$version/$cluster"; |
| 113 |
} |
| 114 |
|
| 115 |
# setup environment |
| 116 |
$ENV{'PGSYSCONFDIR'} //= '/etc/postgresql-common'; |
| 117 |
$ENV{'PGHOST'} = $host if ($host); |
| 118 |
$ENV{'PGPORT'} = $port if $port && !$ENV{'PGPORT'}; |
| 119 |
$ENV{'PGDATABASE'} = $db if $db && !$ENV{'PGDATABASE'}; |
| 120 |
|
| 121 |
# check under which name we were called |
| 122 |
my $cmdname = (split '/', $0)[-1]; |
| 123 |
|
| 124 |
unless ($version or $explicit_host or $explicit_port or $explicit_service) { |
| 125 |
print STDERR "Warning: No existing cluster is suitable as a default target. Please see man pg_wrapper(1) how to specify one.\n"; |
| 126 |
} |
| 127 |
|
| 128 |
# if we have no version yet, use the latest version. If we were called as psql, |
| 129 |
# pg_archivecleanup, or pg_isready, always use latest version |
| 130 |
if (not $version or $cmdname =~ /^(psql|pg_archivecleanup|pg_isready)$/) { |
| 131 |
my $max_version; |
| 132 |
if ($version and $version < 9.2) { # psql 15 only supports PG 9.2+ |
| 133 |
$max_version = 14; |
| 134 |
} |
| 135 |
$version = get_newest_version($cmdname, $max_version); |
| 136 |
} |
| 137 |
unless ($version) { |
| 138 |
error 'You must install at least one postgresql-client-<version> package'; |
| 139 |
} |
| 140 |
error "PostgreSQL version $version is not installed" unless -d "$PgCommon::binroot$version"; |
| 141 |
|
| 142 |
my $cmd; |
| 143 |
if ($cmdname eq 'pg_wrapper') { |
| 144 |
error "pg_wrapper called directly but no program given as argument" |
| 145 |
if (@ARGV == 0); |
| 146 |
$cmd = shift; # will be unshifted back below |
| 147 |
} else { |
| 148 |
$cmd = get_program_path ($cmdname, $version); |
| 149 |
} |
| 150 |
|
| 151 |
error "pg_wrapper: $cmdname was not found in $PgCommon::binroot$version/bin" unless $cmd; |
| 152 |
unshift @ARGV, $cmd; |
| 153 |
exec @ARGV; |
| 154 |
|
| 155 |
__END__ |
| 156 |
|
| 157 |
=head1 NAME |
| 158 |
|
| 159 |
pg_wrapper - wrapper for PostgreSQL client commands |
| 160 |
|
| 161 |
=head1 SYNOPSIS |
| 162 |
|
| 163 |
I<client-program> [B<--cluster> I<version>/I<cluster>] [...] |
| 164 |
|
| 165 |
(I<client-program>: B<psql>, B<createdb>, B<dropuser>, and all other client |
| 166 |
programs installed in C</usr/lib/postgresql/>I<version>C</bin>). |
| 167 |
|
| 168 |
=head1 DESCRIPTION |
| 169 |
|
| 170 |
This program is run only as a link to names which correspond to PostgreSQL |
| 171 |
programs in C</usr/lib/postgresql/>I<version>C</bin>. It determines the |
| 172 |
configured cluster and database for the user and calls the appropriate version |
| 173 |
of the desired program to connect to that cluster and database, supplying any |
| 174 |
specified options to that command. |
| 175 |
|
| 176 |
The target cluster is selected by the following means, in descending order of |
| 177 |
precedence: |
| 178 |
|
| 179 |
=over |
| 180 |
|
| 181 |
=item |
| 182 |
|
| 183 |
explicit specification with the B<--host> option |
| 184 |
|
| 185 |
=item |
| 186 |
|
| 187 |
explicit specification with the B<--cluster> option |
| 188 |
|
| 189 |
=item |
| 190 |
|
| 191 |
if the B<PGHOST> environment variable is set, no further cluster selection is |
| 192 |
performed. The default PostgreSQL version and port number (from the command |
| 193 |
line, the environment variable B<PGPORT>, or default 5432) will be used. |
| 194 |
|
| 195 |
=item |
| 196 |
|
| 197 |
explicit specification with the B<PGCLUSTER> environment variable |
| 198 |
|
| 199 |
=item |
| 200 |
|
| 201 |
if a port is given (either via B<-p>, B<--port>, or B<PGPORT>), and no host is |
| 202 |
given, the local cluster matching that port number is used |
| 203 |
|
| 204 |
=item |
| 205 |
|
| 206 |
matching entry in C<~/.postgresqlrc> (see L<postgresqlrc(5)>), if that |
| 207 |
file exists |
| 208 |
|
| 209 |
=item |
| 210 |
|
| 211 |
matching entry in C</etc/postgresql-common/user_clusters> (see |
| 212 |
L<user_clusters(5)>), if that file exists |
| 213 |
|
| 214 |
=item |
| 215 |
|
| 216 |
If only one cluster exists on the local system, that one will be selected. |
| 217 |
|
| 218 |
=item |
| 219 |
|
| 220 |
If several clusters exist on the local system, the one listening on the default port 5432 |
| 221 |
will be selected. |
| 222 |
|
| 223 |
=back |
| 224 |
|
| 225 |
If none of these rules match, B<pg_wrapper> does not set any environment |
| 226 |
variables and the program called will likely error out with a message like |
| 227 |
"could not connect to server: Connection refused". |
| 228 |
|
| 229 |
For B<psql>, B<pg_archivecleanup>, and B<pg_isready>, B<pg_wrapper> will always use the binary from |
| 230 |
the newest PostgreSQL version installed, as these are downwards compatible. |
| 231 |
If the cluster version is older than 9.2, the newest considered binary version is 14. |
| 232 |
|
| 233 |
Note that B<pg_wrapper> needs to be able to read the server config to get the |
| 234 |
port number to connect to. If a non-standard port is configured in a place that |
| 235 |
pg_wrapper cannot read, connecting will fail. This particularly holds if the |
| 236 |
port was configured via B<ALTER SYSTEM> in C<postgresql.auto.conf> and |
| 237 |
pg_wrapper is invoked as any user other than B<postgres> and B<root>. |
| 238 |
|
| 239 |
=head1 OPTIONS |
| 240 |
|
| 241 |
=over |
| 242 |
|
| 243 |
=item B<--cluster> I<version>B</>I<cluster> |
| 244 |
|
| 245 |
=item B<--cluster> I<version>B</>I<host>B<:>[I<port>] |
| 246 |
|
| 247 |
I<cluster> is either the name of a cluster on the local system, or takes the form |
| 248 |
I<host>:I<port> for a remote cluster. If I<port> is left empty (i. e. you just |
| 249 |
specify I<host:>), it defaults to 5432. |
| 250 |
|
| 251 |
=back |
| 252 |
|
| 253 |
=head1 ENVIRONMENT |
| 254 |
|
| 255 |
=over |
| 256 |
|
| 257 |
=item B<PGCLUSTER> |
| 258 |
|
| 259 |
If C<$PGCLUSTER> is set, its value (of the form I<version>/I<cluster>) |
| 260 |
specifies the desired cluster, similar to the B<--cluster> option. However, if |
| 261 |
B<--cluster> is specified, it overrides the value of C<$PGCLUSTER>. |
| 262 |
|
| 263 |
=item B<PG_CLUSTER_CONF_ROOT> |
| 264 |
|
| 265 |
This specifies an alternative base directory for cluster configurations. This |
| 266 |
is usually C</etc/postgresql/>, but for testing/development purposes you can |
| 267 |
change this to point to e. g. your home directory, so that you can use the |
| 268 |
postgresql-common tools without root privileges. |
| 269 |
|
| 270 |
=item B<PGSYSCONFDIR> |
| 271 |
|
| 272 |
This is the location of PostgreSQL's and postgresql-common's global |
| 273 |
configuration (e. g. C<pg_service.conf>, L<user_clusters(5)>). The default is |
| 274 |
C</etc/postgresql-common/>. |
| 275 |
|
| 276 |
=back |
| 277 |
|
| 278 |
=head1 FILES |
| 279 |
|
| 280 |
=over |
| 281 |
|
| 282 |
=item C</etc/postgresql-common/user_clusters> |
| 283 |
|
| 284 |
stores the default cluster and database for users and groups as set by |
| 285 |
the administrators. |
| 286 |
|
| 287 |
=item C<$HOME/.postgresqlrc> |
| 288 |
|
| 289 |
stores defaults set by the user himself. |
| 290 |
|
| 291 |
=back |
| 292 |
|
| 293 |
=head1 SEE ALSO |
| 294 |
|
| 295 |
L<user_clusters(5)>, L<postgresqlrc(5)> |
| 296 |
|
| 297 |
=head1 AUTHOR |
| 298 |
|
| 299 |
Martin Pitt L<E<lt>[email protected]<gt>> |