title: Can you use pg_connect in PHP to use ONLY local unix-domain sockets?
body: <p>Okay, this is something I've been banging my head on for a few weeks now, so just bear with me. When I set up my postgres database, I wanted to connect to it only through the local Unix socket. Just like how I set up my MySQL database, and subsequent PHP scripts I wrote for it. Thus, the bottom of my pg_hba.conf file looked like this:</p>
  
  <pre><code># TYPE  DATABASE        USER            ADDRESS                 METHOD
  
  # "local" is for Unix domain socket connections only
  local   all             all                                     md5
  # IPv4 local connections:
  #host    all             all             127.0.0.1/32            md5
  # IPv6 local connections:
  #host    all             all             ::1/128                 md5
  </code></pre>
  
  <p>I was, and still am, able to interface with my db via the psql command-line utility, and do anything I need to do without issue. However, when I tried to write a simple PHP script to connect to the db, it would always fail:</p>
  
  <pre><code>&lt;?php
  $dbhost = "localhost";
  $dbname = "postgres";
  $dbuser = "postgres";
  $dbpassword = "####";
  $conn_string = "host=$dbhost dbname=$dbname user=$dbuser password=$dbpassword";
  $dbconn = pg_connect($conn_string);
  if($dbconn)
  {
          echo "&lt;p&gt;Win!&lt;/p&gt;";
  }
  else
  {
          echo "&lt;p&gt;Connect failed!&lt;/p&gt;";
          exit();
  }
  pg_close($dbconn);
  ?&gt;
  </code></pre>
  
  <p>I was absolutely dumbstruck for weeks as to why PHP wouldn't establish a simple Unix socket connection, especially after writing quite a few scripts with the mysqli library for my MySQL db that used local connections! However, I finally un-commented the line associated with the loop-back address, and like magic it suddenly worked! This indicates to me that when 'localhost' is passed as the host parameter of pg_connect, it uses the loop-back interface, rather than just the local Unix socket. </p>
  
  <p>It's not the end of the world, but is there a way to use PHP in a way so that it connects via a local Unix socket, or does it not support this type of connection with postgresql? </p>