5

I'm trying to connect to Heroku Postgres which only support SSL connections. SSL connection works fine from other tools (Postico) and programming environments (Node.js), but when connecting from PHP I always get this error: sslmode value "require" invalid when SSL support is not compiled

My local environment is OS X and all packages are installed with homebrew and have SSL support. Also pgsql has SSL support based on phpinfo() output: SSL support => enabled

Libpq and Postgres are compiled with SSL support: -lpgcommon -lpgport -lssl -lcrypto -lz -lreadline -lm

PHP version: 7.2.5 (also tried 5.6, 7.1 branches) Local Postgres and libpq version: 10.3

Tried every solution I could but can't get this connection working. Postgres support comes compiled out of box for PHP 7.2.5 when installing through homebrew. There is no more separate php-pgsql/php-pdo-pgsql package.

Vilis
  • 1,062
  • 2
  • 12
  • 21
  • Possible duplicate of [How to connect to Heroku postgres database from a local connection in php](https://stackoverflow.com/questions/14983491/how-to-connect-to-heroku-postgres-database-from-a-local-connection-in-php) – Scoots May 10 '18 at 11:50
  • Found that and a few other SO topics, but in my case the SSL support is compiled and looks like the problem is a bit different. From phpinfo(): "OpenSSL support => enabled" – Vilis May 11 '18 at 07:54
  • Retracting flag then :) – Scoots May 11 '18 at 09:13
  • So from local you are trying to connect a remote heroku PSQL? Also what is the output of `php -i | grep -i SSL` – Tarun Lalwani May 18 '18 at 04:41
  • Yes, that is the case. The output is quite large from that command, but here are some SSL related settings: ```SSL support => enabled OpenSSL support => enabled``` And PHP is built with this flag: ```'--with-openssl=/usr/local/opt/openssl'``` – Vilis May 18 '18 at 07:54

2 Answers2

5

The error message is clear, it comes from this code in PostgreSQL's libpq:

#ifndef USE_SSL
        switch (conn->sslmode[0])
        {
            case 'a':           /* "allow" */
            case 'p':           /* "prefer" */

                /*
                 * warn user that an SSL connection will never be negotiated
                 * since SSL was not compiled in?
                 */
                break;

            case 'r':           /* "require" */
            case 'v':           /* "verify-ca" or "verify-full" */
                conn->status = CONNECTION_BAD;
                printfPQExpBuffer(&conn->errorMessage,
                                  libpq_gettext("sslmode value \"%s\" invalid when SSL support is not compiled in\n"),
                                  conn->sslmode);
                return false;
        }
#endif

That code is only compiled when PostgreSQL was not configured --with-openssl.

You can verify that with pg_config (if you didn't install PostgreSQL from source, you may have to install a "dev" or "devel" package for that):

pg_config --configure

The output will not contain --with-openssl.

It may well be that PHP is built with SSL support, but PostgreSQL isn't.

Since you say that PostgreSQL is compiled with SSL support, one explanation for the behavior is that there are several PostgreSQL installations on your machine, and PHP uses one that is build without SSL support. Try and find all files called libpq.* on your system!

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
0

I ran into the same issue connecting to PostgreSQL on Heroku using MAMP with PHP7.2.1. Kept getting "sslmode value "require" invalid when SSL support is not compiled" even though openSSL and pgsql SSL support were showing as enabled in phpinfo(). Installing postgresql via homebrew fixed the issue for me.

brew install postgresql

Wanted to post in case this works for anyone else running into the same problem.

John F
  • 429
  • 6
  • 5