--- postgresql-8.2.5/src/bin/pg_ctl/pg_ctl.c.orig 2007-09-18 22:08:39.757996485 +0100 +++ postgresql-8.2.5/src/bin/pg_ctl/pg_ctl.c 2007-09-18 22:43:12.441795575 +0100 @@ -19,6 +19,7 @@ #include "postgres_fe.h" #include "libpq-fe.h" +#include "pg_config_manual.h" #include #include @@ -404,10 +405,12 @@ bool success = false; int i; char portstr[32]; + char sockstr[MAXPGPATH]; char *p; - char connstr[128]; /* Should be way more than enough! */ + char connstr[MAXPGPATH+128]; *portstr = '\0'; + *sockstr = '\0'; /* post_opts */ for (p = post_opts; *p;) @@ -432,7 +435,7 @@ } /* config file */ - if (!*portstr) + if (!*portstr || !*sockstr) { char **optlines; @@ -445,18 +448,32 @@ while (isspace((unsigned char) *p)) p++; - if (strncmp(p, "port", strlen("port")) != 0) - continue; - p += strlen("port"); - while (isspace((unsigned char) *p)) - p++; - if (*p != '=') - continue; - p++; - while (isspace((unsigned char) *p)) + if (!*portstr && strncmp(p, "port", strlen("port")) == 0) + { + p += strlen("port"); + while (isspace((unsigned char) *p)) + p++; + if (*p != '=') + continue; p++; - StrNCpy(portstr, p, Min(strcspn(p, "#" WHITESPACE) + 1, + while (isspace((unsigned char) *p)) + p++; + StrNCpy(portstr, p, Min(strcspn(p, "#" WHITESPACE) + 1, sizeof(portstr))); + } + if (!*sockstr && strncmp(p, "unix_socket_directory", strlen("unix_socket_directory")) == 0) + { + p += strlen("unix_socket_directory"); + while (isspace((unsigned char) *p)) + p++; + if (*p != '=') + continue; + p++; + while (isspace((unsigned char) *p) || *p == '\'' || *p == '"') + p++; + StrNCpy(sockstr, p, Min(strcspn(p, "#\"'" WHITESPACE) + 1, + sizeof(sockstr))); + } /* keep looking, maybe there is another */ } } @@ -473,6 +490,12 @@ /* We need to set a connect timeout otherwise on Windows the SCM will probably timeout first */ snprintf(connstr, sizeof(connstr), "dbname=postgres port=%s connect_timeout=5", portstr); + /* path to directory containing the unix socket */ + if (*sockstr) { + strncat(connstr, " host=", sizeof(connstr) - strlen(connstr)); + strncat(connstr, sockstr, sizeof(connstr) - strlen(connstr)); + } + for (i = 0; i < wait_seconds; i++) { if ((conn = PQconnectdb(connstr)) != NULL &&