--- /usr/share/postgresql-common/pg_wrapper 2015-05-07 00:25:15.000000000 +0300 +++ /tmp/pg_wrapper 2018-10-05 22:28:30.414793594 +0300 @@ -32,7 +32,8 @@ } # Check for --cluster argument and filter it out, and check if --port is specified -my $port_specified = exists $ENV{'PGPORT'}; +my $port_specified = (exists $ENV{'PGPORT'})?$ENV{'PGPORT'}:0; + for (my $i = 0; $i <= $#ARGV; ++$i) { last if $ARGV[$i] eq '--'; @@ -56,10 +57,33 @@ last; } - $port_specified = 1 if $ARGV[$i] =~ /^--port\b/ || $ARGV[$i] =~ /^-\w*p\w*\d*$/; + if ($ARGV[$i] =~ /^--port\b/) { + $port_specified = $ARGV[$i+1]; + } elsif ($ARGV[$i] =~ /^-\w*p\w*(\d*)$/){ + $port_specified = $1 || $ARGV[$i+1];; + } + $host = '.from.commandline' if $ARGV[$i] =~ /^--host\b/ || $ARGV[$i] =~ /^-\w*h\w*$/; } +#Determine version and cluster from port if specified, and host is not specified +if ($port_specified and (!$host or $host eq 'locahost')) { + my %ports; + for my $v (get_versions()) { + for my $c (get_version_clusters $v) { + my $port = get_cluster_port($v, $c); + $ports{$port}=[$c,$v]; + } + } + my $sel=$ports{$port_specified}; + error "port $port_specified unrecognised" unless($sel); + if ($cluster and $cluster ne $$sel[0]) { + error "Specified port is for cluster ".$$sel[0].'/'.$$sel[1]; + } else { + ($cluster,$version)=@{$sel}; + } +} + # Determine $version, $cluster, $db, $port from map files ($version, $cluster, $db) = user_cluster_map() unless $cluster;