From d9236a69fc2c510f8f165cba3bd9c991440c1ee0 Mon Sep 17 00:00:00 2001 From: Tom Lane 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 b355e303ca..cf2cbcceaa 100644 --- a/doc/src/sgml/libpq.sgml +++ b/doc/src/sgml/libpq.sgml @@ -1,5 +1,5 @@ @@ -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 - /tmp.Unix domain - socket + default behavior when host is not specified + is to connect to a Unix-domain + socketUnix domain socket in + /tmp (or whatever socket directory was specified + when PostgreSQL was built). On machines without + Unix-domain sockets, the default is to connect to localhost. @@ -150,7 +153,8 @@ PGconn *PQconnectdb(const char *conninfo); Without either a host name or host address, libpq 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 localhost. @@ -3644,6 +3648,12 @@ to avoid DNS lookup overhead. See the documentation of these parameters, under PQconnectdb above, for details on their interaction. + +When neither PGHOST nor PGHOSTADDR is set, +the default behavior is to connect using a local Unix-domain socket; or on +machines without Unix-domain sockets, libpq will +attempt to connect to localhost. + diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 92fdd62ef2..53786ebf0a 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -238,7 +238,7 @@ PostgreSQL documentation - 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 PGPORT environment variable or, if not set, to the port specified at @@ -489,19 +489,24 @@ PostgreSQL documentation , , , and 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, psql will connect via a Unix domain socket - to a server on the local host, or via TCP/IP to 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, psql will connect via a Unix-domain socket + to a server on the local host, or via TCP/IP to 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. + + + + When the defaults aren't quite right, you can save yourself + some typing by setting the environment variables PGDATABASE, PGHOST, - PGPORT and PGUSER to appropriate + PGPORT and/or PGUSER to appropriate values. diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 656b9f11d7..4def5ccb94 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 57d7ee8e6f..f28ca97e36 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);