Fixing PostgreSQL Connection Issues in Laravel Valet on macOS

Laravel Valet is a lightweight and efficient tool for local PHP development on macOS, but it can run into issues when used with PostgreSQL. One common problem is the premature closing of connections, often due to encryption conflicts involving PostgreSQL’s GSSAPI.

The Issue

PostgreSQL tries to use GSSAPI encryption by default, which can cause connection problems on macOS due to its stringent security model. Laravel Valet’s default setup doesn’t configure this encryption, leading to premature connection termination.

Solution: Disable GSSAPI Encryption

To resolve this, you’ll need to disable GSSAPI encryption in PostgreSQL by modifying the valet-fpm.conf file across all active PHP versions.

Steps to Fix:

  1. Edit the valet-fpm.conf File:
    Modify the configuration for each active PHP version (e.g., PHP 8.2 and PHP 8.1):
sudo nano /usr/local/etc/php/8.2/php-fpm.d/valet-fpm.conf
sudo nano /usr/local/etc/php/8.1/php-fpm.d/valet-fpm.conf
  1. Add the PGGSSENCMODE Variable: Disable GSSAPI encryption by adding this line in the file:
    env['PGGSSENCMODE'] = disable
    
  2. Restart Valet: Apply the changes by restarting Laravel Valet:
    valet restart
    

By following these steps, you should resolve the connection issues with PostgreSQL in your Laravel Valet environment on macOS.