From d9236a69fc2c510f8f165cba3bd9c991440c1ee0 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 28 Dec 2004 23:17:54 +0000
Subject: [PATCH] Make libpq default to localhost connections on machines
 without Unix-domain sockets, rather than failing as it formerly did.  Revert
 the thereby-obsoleted patch to make psql supply the localhost default.

---
 doc/src/sgml/libpq.sgml           | 20 +++++++++++++++-----
 doc/src/sgml/ref/psql-ref.sgml    | 25 +++++++++++++++----------
 src/bin/psql/startup.c            |  7 +------
 src/interfaces/libpq/fe-connect.c | 12 ++++++++----
 4 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index b355e303cac..cf2cbcceaa8 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.173 2004/12/28 22:47:15 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/libpq.sgml,v 1.174 2004/12/28 23:17:18 tgl Exp $
 -->
 
  <chapter id="libpq">
@@ -110,9 +110,12 @@ PGconn *PQconnectdb(const char *conninfo);
       If this begins with a slash, it specifies Unix-domain
       communication rather than TCP/IP communication; the value is the
       name of the directory in which the socket file is stored.  The
-      default is to connect to a Unix-domain socket in
-      <filename>/tmp</filename>.<indexterm><primary>Unix domain
-      socket</></>
+      default behavior when <literal>host</literal> is not specified
+      is to connect to a Unix-domain
+      socket<indexterm><primary>Unix domain socket</></> in
+      <filename>/tmp</filename> (or whatever socket directory was specified
+      when <productname>PostgreSQL</> was built). On machines without
+      Unix-domain sockets, the default is to connect to <literal>localhost</>.
      </para>
      </listitem>
     </varlistentry>
@@ -150,7 +153,8 @@ PGconn *PQconnectdb(const char *conninfo);
      <para>
       Without either a host name or host address,
       <application>libpq</application> will connect using a
-      local Unix domain socket.
+      local Unix-domain socket; or on machines without Unix-domain
+      sockets, it will attempt to connect to <literal>localhost</>.
      </para>
      </listitem>
     </varlistentry>
@@ -3644,6 +3648,12 @@ to avoid DNS lookup overhead. See the documentation of
 these parameters, under <function>PQconnectdb</function> above, for details
 on their interaction.
 </para>
+<para>
+When neither <envar>PGHOST</envar> nor <envar>PGHOSTADDR</envar> is set,
+the default behavior is to connect using a local Unix-domain socket; or on
+machines without Unix-domain sockets, <application>libpq</application> will
+attempt to connect to <literal>localhost</>.
+</para>
 </listitem>
 <listitem>
 <para>
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 92fdd62ef20..53786ebf0ab 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1,5 +1,5 @@
 <!--
-$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.125 2004/12/27 20:13:48 tgl Exp $
+$PostgreSQL: pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.126 2004/12/28 23:17:38 tgl Exp $
 PostgreSQL documentation
 -->
 
@@ -238,7 +238,7 @@ PostgreSQL documentation
       <term><option>--port <replaceable class="parameter">port</replaceable></></term>
       <listitem>
       <para>
-      Specifies the TCP port or the local Unix domain
+      Specifies the TCP port or the local Unix-domain
       socket file extension on which the server is listening for
       connections. Defaults to the value of the <envar>PGPORT</envar>
       environment variable or, if not set, to the port specified at
@@ -489,19 +489,24 @@ PostgreSQL documentation
     <option>-d</option>, <option>-h</option>, <option>-p</option>, and
     <option>-U</option> respectively. If an argument is found that does
     not belong to any option it will be interpreted as the database name
-    (or the user name, if the database name is also given). Not all
-    these options are required, defaults do apply. If you omit the host
-    name, <application>psql</> will connect via a Unix domain socket
-    to a server on the local host, or via TCP/IP to <literal>localhost</> on machines
-    that don't have Unix domain sockets. The default port number is compile-time determined.
+    (or the user name, if the database name is already given). Not all
+    these options are required; there are useful defaults. If you omit the host
+    name, <application>psql</> will connect via a Unix-domain socket
+    to a server on the local host, or via TCP/IP to <literal>localhost</> on
+    machines that don't have Unix-domain sockets. The default port number is
+    determined at compile time.
     Since the database server uses the same default, you will not have
     to specify the port in most cases. The default user name is your
     Unix user name, as is the default database name. Note that you can't
     just connect to any database under any user name. Your database
-    administrator should have informed you about your access rights. To
-    save you some typing you can also set the environment variables
+    administrator should have informed you about your access rights.
+    </para>
+
+    <para>
+    When the defaults aren't quite right, you can save yourself
+    some typing by setting the environment variables
     <envar>PGDATABASE</envar>, <envar>PGHOST</envar>,
-    <envar>PGPORT</envar> and <envar>PGUSER</envar> to appropriate
+    <envar>PGPORT</envar> and/or <envar>PGUSER</envar> to appropriate
     values.
     </para>
 
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 656b9f11d75..4def5ccb944 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2000-2004, PostgreSQL Global Development Group
  *
- * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.106 2004/11/27 18:51:07 tgl Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.107 2004/12/28 23:17:41 tgl Exp $
  */
 #include "postgres_fe.h"
 
@@ -159,11 +159,6 @@ main(int argc, char *argv[])
 	pset.getPassword = false;
 #endif
 
-#ifndef HAVE_UNIX_SOCKETS
-	/* default to localhost on platforms without unix sockets */
-	options.host = "localhost";
-#endif
-
 	parse_psql_options(argc, argv, &options);
 
 	if (!pset.popt.topt.fieldSep)
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index 57d7ee8e6ff..f28ca97e368 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.292 2004/12/02 23:20:19 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/interfaces/libpq/fe-connect.c,v 1.293 2004/12/28 23:17:54 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -868,7 +868,7 @@ connectDBStart(PGconn *conn)
 	char		portstr[128];
 	struct addrinfo *addrs = NULL;
 	struct addrinfo hint;
-	const char *node = NULL;
+	const char *node;
 	int			ret;
 
 	if (!conn)
@@ -907,15 +907,19 @@ connectDBStart(PGconn *conn)
 		node = conn->pghost;
 		hint.ai_family = AF_UNSPEC;
 	}
-#ifdef HAVE_UNIX_SOCKETS
 	else
 	{
+#ifdef HAVE_UNIX_SOCKETS
 		/* pghostaddr and pghost are NULL, so use Unix domain socket */
 		node = NULL;
 		hint.ai_family = AF_UNIX;
 		UNIXSOCK_PATH(portstr, portnum, conn->pgunixsocket);
-	}
+#else
+		/* Without Unix sockets, default to localhost instead */
+		node = "localhost";
+		hint.ai_family = AF_UNSPEC;
 #endif   /* HAVE_UNIX_SOCKETS */
+	}
 
 	/* Use getaddrinfo_all() to resolve the address */
 	ret = getaddrinfo_all(node, portstr, &hint, &addrs);
-- 
GitLab