Search code examples
phppostgresqlmacosfpm

PHP pg_connect() does not return in PHP-FPM after MacOS Sonoma Upgrade


I have wasted a lot of time on this and I'm stumped. Anyone have some insight or work arounds? Thanks in advance for your attention...

My PHP/PostgreSQL/NGINX app has been running on MacOS using PHP-FPM installed via Homebrew for years. After upgrading MacOS to Sonoma 14.1 from 13.x, it no longer returns from pg_connect(). I've replicated this on 2 machines as well.

No PHP, NGINX, or PostgreSQL configurations have changed. Only the MacOS upgrade. PostgreSQL connects properly using psql and different --host parameters to access by the same interface.

I created a test script with just the pg_connect(), trying multiple permutations and discovered:

<?php
# The following line does not return and the request stops
$db = pg_connect("host=dbserver port=5432 dbname=appdb user=appuser");
echo "success!"
?>
  • Both pg_connect and pg_pconnect die or don't return from NGINX/PHP-FPM when invoking the test script (NGINX returns "Bad Gateway).

  • Both work when invoking the test script from command line (php -f test.php)

  • Removing the "host=xxx" parameter from the connection string works for both cases, and it is able to connect to the local postgresql (not what I need though).

  • Variations "host=ip", "host=127.0.0.1", "host=dnsname", "host=localhost" all die or don't return. (FPM)

  • I can't find any useful errors in Postgres, NGINX or PHP logs, but in php-fpm.log, I received thousands of these messages:

    WARNING: [pool www] child 82817 exited on signal 9 (SIGKILL) after 0.000208 seconds from start

Could MacOS now be preventing the connection until set to allow?

Versions: MacOS 14.1.1, PHP 8.2.13 also 8.3.0, PostgreSQL 15.5 server, also 16.1. libpq 16.1, nginx/1.25.3, Homebrew 4.1.21; Apple Mac M1


Solution

  • Finally I've found the solution here: https://github.com/laravel/valet/issues/1433#issuecomment-1741308660

    Add following lines to /opt/homebrew/etc/php/8.3/php-fpm.d/www.conf:

    env['PGGSSENCMODE'] = disable
    env['LC_ALL'] = C
    

    Then restart PHP. And it starts working!