diff --git a/debian/changelog b/debian/changelog
index 089e3392874ca96acc9cd4fd0879144479472a8e..626fb7a233457f4173daab34ed3f9a2e1c3bd919 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -14,6 +14,8 @@ postgresql-common (139) UNRELEASED; urgency=low
   * PgCommon.pm, pg_ctlcluster, pg_createcluster, t/030_errors.t: 9.3 renames
     unix_socket_directory to unix_socket_directories.
   * pg_wrapper: Document that PGHOST overrides PGCLUSTER. (Closes: #697291)
+  * pg_wrapper: Skip cluster selection if --host is given on the command line.
+    (Closes: #637017) While we are at it, improve --port parsing, too.
 
   [ Martin Pitt ]
   * debian/supported-versions: Read /etc/os-release if present, and fall back
diff --git a/pg_wrapper b/pg_wrapper
index 7e2113e35dbe1ea01870f82617beec3dc0f4ba4e..56b6a8406bbd370ad4655d55c5ebbbead9cdc0b5 100755
--- a/pg_wrapper
+++ b/pg_wrapper
@@ -51,7 +51,8 @@ for (my $i = 0; $i <= $#ARGV; ++$i) {
         last;
     }
 
-    $port_specified = 1 if $ARGV[$i] eq '--port' || $ARGV[$i] =~ /^-\w*p\w*$/;
+    $port_specified = 1 if $ARGV[$i] =~ /^--port\b/ || $ARGV[$i] =~ /^-\w*p\w*\d*$/;
+    $host = '.from.commandline' if $ARGV[$i] =~ /^--host\b/ || $ARGV[$i] =~ /^-\w*h\w*$/;
 }
 
 # Determine $version, $cluster, $db, $port from map files
@@ -79,7 +80,7 @@ if (!$host && $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;
+$ENV{'PGHOST'} = $host if $host && $host ne '.from.commandline';
 
 # if we only have a port, but no version here, use the latest version
 # TODO: this could be improved by better argument parsing and mapping back the
@@ -170,33 +171,37 @@ precedence:
 
 =item 1.
 
-explicit specification with the B<--cluster> option
+explicit specification with the B<--host> option
 
 =item 2.
 
-if the B<PGHOST> environment variable is set, no further cluster selection is
-performed. The default PostgreSQL version and port number (from the environment
-variable B<PGPORT>, or default 5432) will be used.
+explicit specification with the B<--cluster> option
 
 =item 3.
 
-explicit specification with the B<PGCLUSTER> environment variable
+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.
 
 =item 4.
 
+explicit specification with the B<PGCLUSTER> environment variable
+
+=item 5.
+
 matching entry in C<~/.postgresqlrc> (see L<postgresqlrc(5)>), if that
 file exists
 
-=item 5.
+=item 6.
 
 matching entry in C</etc/postgresql-common/user_clusters> (see
 L<user_clusters(5)>), if that file exists
 
-=item 6.
+=item 7.
 
 If only one local cluster exists, that one will be selected.
 
-=item 7.
+=item 8.
 
 If several local clusters exist, the one listening on the default port 5432
 will be selected.