title: How to add the pdo_pgsql extension to php7.1 on MacOs Sierra
body: <p>I'm running macOS Sierra (10.12.6) and php7.1 and for way to long I have been trying to figure out how to read a php file that contains the PDO::FETCH_ASSOC. I'm getting an error which I (think) is related to not having php configured correctly with the extension pdo_pgsql. The php file looks like this:</p>
  
  <pre><code>&lt;?php 
  error_reporting(E_ALL);
  ini_set('display_errors', '1');
  $db = new PDO('pgsql:host=localhost;port=5432;dbname=some_db;', 'username', 'password');
  $sql = $db-&gt;query("SELECT id, name, image, web, category, ST_AsGeoJSON(geom, 5) as geom FROM some_file ORDER BY name");
  $features=[];
  while ($row=$sql-&gt;fetch(PDO::FETCH_ASSOC)){
      $feature=['type'=&gt;'Feature'];
      $feature['geometry']=json_decode($row['geom']);
      unset($row['geom']);
      $feature['properties']=$row;
      array_push($features, $feature);
  }
  $featureCollection=['type'=&gt;'FeatureCollection', 'features'=&gt;$features];
  echo json_encode($featureCollection);
  ?&gt;
  </code></pre>
  
  <p>and the part of the html file that is referring to it is:</p>
  
  <pre><code>        &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;WebMap 101&lt;/title&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"&gt;
      &lt;link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css"&gt;
      &lt;script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"&gt;&lt;/script&gt;
      &lt;script src="https://code.jquery.com/jquery-2.2.4.min.js"&gt;&lt;/script&gt;
      &lt;script src="resources/leaflet.ajax.js"&gt;&lt;/script&gt;
      &lt;script src="resources/turf.min.js"&gt;&lt;/script&gt;
      &lt;style&gt;
          #header {
              height: 75px;
              background-color: darkgoldenrod;
          }
  
          #mapdiv {
              height: 650px;
              background-color: salmon;
          }
  
          #side_panel {
              height: 650px;
              background-color: beige;
          }
  
          #footer {
              height: 75px;
              background-color: darkgray
          }
  
          .attraction {
              margin-bottom: 5px;
          }
  
      &lt;/style&gt;
  &lt;/head&gt;
  
  
  
  &lt;body&gt;
      &lt;div id="header" class="col-md-12"&gt;
          &lt;h1 class="text-center"&gt;Mexico City&lt;/h1&gt;
      &lt;/div&gt;
      &lt;div id="side_panel" class="col-md-3"&gt;
          &lt;h1 class="text-center"&gt;Attractions&lt;/h1&gt;
          &lt;button id="zoomToZocalo" class="form-control btn-primary"&gt;Zocalo&lt;/button&gt;
      &lt;/div&gt;
      &lt;div id="mapdiv" class="col-md-9"&gt;&lt;/div&gt;
      &lt;div id="footer" class="col-md-12"&gt;
          &lt;h4 id="map_coords" class="text-center"&gt;Latitude: 19.4 Longitude: -99.1 Zoom Level: 11&lt;/h4&gt;
          &lt;h4 class="text-center"&gt;&amp;copy;2016 &lt;a href="http:page_name.com"&gt;Page name&lt;/a&gt;&lt;/h4&gt;
      &lt;/div&gt;
      &lt;script&gt;
          var mymap = L.map('mapdiv')
          mymap.setView([19.4, -99.1], 11);
  
          var backgroundLayer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png');
          mymap.addLayer(backgroundLayer);
  
  
  
  
          var zocaloMarker = L.marker([19.43278, -99.13333]).addTo(mymap).bindPopup("&lt;h3 class='text-center'&gt;Zocalo&lt;/h3&gt;&lt;a href='https://en.wikipedia.org/wiki/Z%C3%B3calo' target='blank'&gt;&lt;img src='img/zocalo.jpg' width='200px'&gt;&lt;/a&gt;");
  
          $("#zoomToZocalo").click(function() {
              mymap.setView([19.43278, -99.13333], 17);
          });
  
  
          var lyrAttractions;
          $.ajax({
              url: 'load_attractions.php',
              success: function(response) {
                  if (lyrAttractions) {
                      mymap.removeLayer(lyrAttractions)
                  };
                  lyrAttractions = L.geoJSON(JSON.parse(response), {
                      pointToLayer: function(feature, latlng) {
                          var str = "&lt;button id='zoomTo" + feature.properties.name.replace(/ /g, '');
                          str += "' class='form-control btn btn-primary attraction'&gt;";
                          str += feature.properties.name + "&lt;/button&gt;";
                          $("#side_panel").append(str);
                          $("#zoomTo" + feature.properties.name.replace(/ /g, '')).click(function() {
                              mymap.setView([latlng.lat, latlng.lng], 17);
                          });
  
  
                          var str = "&lt;h4 class='text-center'&gt;" + feature.properties.name + "&lt;/h4&gt;&lt;hr&gt;";
                          str += "&lt;a href='" + feature.properties.web + "' target='blank'&gt;";
                          str += "&lt;img src='img/" + feature.properties.image + "' width='200px'&gt;";
                          str += "&lt;/a&gt;";
                          return L.marker(latlng).bindPopup(str);
  
                  }});
                  lyrAttractions.addTo(mymap);
                  mymap.fitBounds(lyrAttractions.getBounds());
              }});
  
          mymap.on('mousemove', function(e) {
              var str = "Latitude: " + e.latlng.lat.toFixed(5) + " Longitude: " + e.latlng.lng.toFixed(5) + " Zoom Level: " + mymap.getZoom();
              $("#map_coords").html(str);
          });
      &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>In Google developer tool I get this error:</p>
  
  <pre><code>VM102:1 Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0
  at JSON.parse (&lt;anonymous&gt;)
  at Object.success ((index):80)
  at i (jquery-2.2.4.min.js:2)
  at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2)
  at z (jquery-2.2.4.min.js:4)
  at XMLHttpRequest.&lt;anonymous&gt; (jquery-2.2.4.min.js:4)
  </code></pre>
  
  <p>Line 80 is corresponding to the above <code>"lyrAttractions = L.geoJSON(JSON.parse(response), {"</code></p>
  
  <p>The network tab shows:</p>
  
  <p><a href="https://i.stack.imgur.com/yZwLa.png" rel="nofollow noreferrer">Image: Network tab in Google Developer</a></p>
  
  <p>And the Network -> Respons-tab for load_attraction.php show:</p>
  
  <pre><code>&lt;br /&gt;
  &lt;font size='1'&gt;&lt;table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#f57900' colspan="5"&gt;&lt;span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'&gt;( ! )&lt;/span&gt; Fatal error: Uncaught Error: Call to a member function fetch() on boolean in /Users/agh/Sites/webmaps101/load_attractions.php on line &lt;i&gt;7&lt;/i&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#f57900' colspan="5"&gt;&lt;span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'&gt;( ! )&lt;/span&gt; Error: Call to a member function fetch() on boolean in /Users/agh/Sites/webmaps101/load_attractions.php on line &lt;i&gt;7&lt;/i&gt;&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='left' bgcolor='#e9b96e' colspan='5'&gt;Call Stack&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;th align='center' bgcolor='#eeeeec'&gt;#&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Time&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Memory&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Function&lt;/th&gt;&lt;th align='left' bgcolor='#eeeeec'&gt;Location&lt;/th&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;1&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='center'&gt;0.0245&lt;/td&gt;&lt;td bgcolor='#eeeeec' align='right'&gt;362600&lt;/td&gt;&lt;td bgcolor='#eeeeec'&gt;{main}(  )&lt;/td&gt;&lt;td title='/Users/agh/Sites/webmaps101/load_attractions.php' bgcolor='#eeeeec'&gt;.../load_attractions.php&lt;b&gt;:&lt;/b&gt;0&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;&lt;/font&gt;
  </code></pre>
  
  <p>Now over 60 hours later I still haven't found a solution, and starting to get depressed deluxe. My assumption has been that the php extension pdo_pgsql is not working properly. I installed php@7.1 with homebrew using <code>brew install php@7.1</code>, </p>
  
  <p>Here are some stuff that might be helpful:
  <code>$ php -v</code> returns:</p>
  
  <pre><code>Copyright (c) 1997-2018 The PHP Group
  Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
      with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
      with Zend OPcache v7.1.30, Copyright (c) 1999-2018, by Zend Technologies
  </code></pre>
  
  <p><code>$ which php</code> returns:</p>
  
  <pre><code>/usr/local/opt/php@7.1/bin/php
  </code></pre>
  
  <p>and</p>
  
  <p><code>$ php -m | grep pdo_pgsql</code> returns:
  <code>pdo_pgsql</code></p>
  
  <p>and 
  <code>$ php --ini</code> returns:</p>
  
  <pre><code>Configuration File (php.ini) Path: /usr/local/etc/php/7.1
  Loaded Configuration File:         /usr/local/etc/php/7.1/php.ini
  Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
  Additional .ini files parsed:      /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
  </code></pre>
  
  <p><code>$ brew doctor</code> returns:</p>
  
  <pre><code>Warning: "config" scripts exist outside your system or Homebrew directories.
  `./configure` scripts often look for *-config scripts to determine if
  software packages are installed, and which additional flags to use when
  compiling and linking.
  
  Having additional scripts in your path can confuse software installed via
  Homebrew if the config script overrides a system or Homebrew-provided
  script of the same name. We found the following "config" scripts:
    /usr/local/php5/bin/curl-config
    /usr/local/php5/bin/freetype-config
    /usr/local/php5/bin/icu-config
    /usr/local/php5/bin/iodbc-config
    /usr/local/php5/bin/libmcrypt-config
    /usr/local/php5/bin/libpng-config
    /usr/local/php5/bin/libpng12-config
    /usr/local/php5/bin/php-config
    /usr/local/php5/bin/xslt-config
    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3-config
    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6-config
    /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6m-config
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3-config
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7-config
    /Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7m-config
    /Applications/Postgres.app/Contents/Versions/latest/bin/gdal-config
  
  Warning: You have unlinked kegs in your Cellar.
  Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
  those kegs to fail to run properly once built. Run `brew link` on these:
    git
    python
  </code></pre>
  
  <p>Running the phpinfo-file gives me this:</p>
  
  <pre><code>Server API  Apache 2.0 Handler
  Virtual Directory Support   disabled
  Configuration File (php.ini) Path   /usr/local/etc/php/7.1
  Loaded Configuration File   /usr/local/etc/php/7.1/php.ini
  Scan this dir for additional .ini files /usr/local/etc/php/7.1/conf.d
  Additional .ini files parsed    /usr/local/etc/php/7.1/conf.d/ext-opcache.ini
  PHP API 20160303
  PHP Extension   20160303
  Zend Extension  320160303
  Zend Extension Build    API320160303,NTS
  PHP Extension Build API20160303,NTS
  </code></pre>
  
  <p>and further down in the same file:</p>
  
  <pre><code>pdo_pgsql
  PDO Driver for PostgreSQL   enabled
  PostgreSQL(libpq) Version   11.3
  Module version  7.1.30
  Revision    $Id: 9c5f356c77143981d2e905e276e439501fe0f419 $
  </code></pre>
  
  <p>Running <code>pear config-show</code> returns:</p>
  
  <pre><code>Auto-discover new Channels     auto_discover    &lt;not set&gt;
  Default Channel                default_channel  pear.php.net
  HTTP Proxy Server Address      http_proxy       &lt;not set&gt;
  PEAR server [DEPRECATED]       master_server    pear.php.net
  Default Channel Mirror         preferred_mirror pear.php.net
  Remote Configuration File      remote_config    &lt;not set&gt;
  PEAR executables directory     bin_dir          /usr/local/opt/php@7.1/bin
  PEAR documentation directory   doc_dir          /usr/local/share/pear@7.1/doc
  PHP extension directory        ext_dir          /usr/local/lib/php/pecl/20160303/
  PEAR directory                 php_dir          /usr/local/share/pear@7.1
  PEAR Installer cache directory cache_dir        /private/tmp/pear/cache
  PEAR configuration file        cfg_dir          /usr/local/share/pear@7.1/cfg
  directory
  PEAR data directory            data_dir         /usr/local/share/pear@7.1/data
  PEAR Installer download        download_dir     /private/tmp/pear/download
  directory
  Systems manpage files          man_dir          /usr/local/share/man
  directory
  PEAR metadata directory        metadata_dir     &lt;not set&gt;
  PHP CLI/CGI binary             php_bin          /usr/local/opt/php@7.1/bin/php
  php.ini location               php_ini          /usr/local/etc/php/7.1/php.ini
  --program-prefix passed to     php_prefix       &lt;not set&gt;
  PHP's ./configure
  --program-suffix passed to     php_suffix       &lt;not set&gt;
  PHP's ./configure
  PEAR Installer temp directory  temp_dir         /private/tmp/pear/temp
  PEAR test directory            test_dir         /usr/local/share/pear@7.1/test
  PEAR www files directory       www_dir          /usr/local/share/pear@7.1/htdocs
  Cache TimeToLive               cache_ttl        3600
  Preferred Package State        preferred_state  stable
  Unix file mask                 umask            22
  Debug Log Level                verbose          1
  PEAR password (for             password         &lt;not set&gt;
  maintainers)
  Signature Handling Program     sig_bin          /usr/local/bin/gpg
  Signature Key Directory        sig_keydir       /usr/local/etc/php/7.1/pearkeys
  Signature Key Id               sig_keyid        &lt;not set&gt;
  Package Signature Type         sig_type         gpg
  PEAR username (for             username         &lt;not set&gt;
  maintainers)
  User Configuration File        Filename         /Users/agh/.pearrc
  System Configuration File      Filename         /usr/local/etc/php/7.1/pear.conf
  </code></pre>
  
  <p>I've tried php7.3 and php7.2 as well, but encounter the problem that Module API(=20160303)and PHP API(=20170718) don't match and therefore the extension pso_pgsql can't be loaded. When I installed php7.1 (which is using the same API as the module), at least that problem was gone. </p>
  
  <p>However, when I add <code>extension=pdo_pgsql.so</code> in the php.ini file and run <code>php --ini</code>I get an error saying <code>PHP Warning:  Module 'pdo_pgsql' already loaded in Unknown on line 0</code></p>
  
  <p>I've read that there might be another file where pdo_pgsql.so is loaded from and some suggest looking in conf.d folder. I used to have several files in conf.d (think it was during previous php-installations (7.2 and 7.3), e.g "ext-pdo_pgsql.ini". Now I only have "exe-opcache.ini" which only contains information of where the opcache.so files is located. But is there any other place where pdo_pgsql.so can be loaded from?</p>
  
  <p>I've noticed that the icon of the file pdo_pgsql.so differs from the icons connected to extensions that I have installed just recently with pecl. pdo_pgsql has a black terminal with text "exec" and the other extensions just white papers. I can't remember when I got the pdo_pgsql.so file in the first place (since I've tried so many things), because when trying to <code>pecl install pdo_pgsql</code>I end up with an error (finally) saying:</p>
  
  <pre><code>Installing shared extensions:     /private/tmp/pear/temp/pear-build-aghlTd4GE/install-PDO-1.0.3/usr/local/Cellar/php@7.1/7.1.30_1/pecl/20160303/
  cp: modules/*: No such file or directory
  make: *** [install-modules] Error 1
  ERROR: `make INSTALL_ROOT="/private/tmp/pear/temp/pear-build-aghlTd4GE/install-PDO-1.0.3" install' failed
  </code></pre>
  
  <p>I think I might have got pdo_pgsql.so from running the script found here: <a href="https://gist.github.com/doole/8651341" rel="nofollow noreferrer">https://gist.github.com/doole/8651341</a> and it seems to work with a few modifications. It finally ends up with returning: </p>
  
  <pre><code>extension=/usr/local/lib/php/extensions/pdo_pgsql.so
  </code></pre>
  
  <p>I manually move the file in the same ext_dir folder given by <code>pear config-show</code> (but have also tried to change the ext_dir to <code>/usr/local/lib/php/extensions/</code> without luck.)</p>
  
  <p>But, since pdo_pgsql is presented when running <code>$ php -m</code> I assume it is already(?) included in the php package? But how on earth can I enable it to load in the php.ini file, without the error that it has already being loaded? On the other hand, it says in the phpinfo-file that PDO driver for PostgreSQL is enabled. Does it load by default, without telling it to do so in php.ini? Is that why it is "already loaded"?  </p>
  
  <p>Soo... am I looking in the wrong direction? Is my original error PDO::FETCH_ASSOC caused by something else? If so: what on earth can it be?</p>
  
  <p>I would be very happy if someone could provide some advice. I'm out of ideas.</p>