0

Struggling to configure this correctly and cleanly. When I upgrade to latest 3.x Craft on staging, Craft can no longer connect to the Postgres DB, which uses SSL. Have had success up until CraftCMS version 3.7.28 by overwriting vendor/craftcms/cms/src/config/DbConfig.php and with a config/db.php as follows:

<?php
return [
    'dsn' => 'pgsql:host=' . getenv('DB_SERVER') .
         ';port=' . getenv('DB_PORT') . 
         ';dbname=' . getenv('DB_DATABASE') .
         ';sslmode=require' . 
         ';sslcert=/etc/ssl/certs/ssl-cert-pg.pem' . 
         ';sslkey=/etc/ssl/private/ssl-cert-pg.key' .
         ';sslrootcert=/etc/ssl/certs/ssl-cert-pg.pem' .
         ';user=' . getenv('DB_USER') .
         ';password=' . getenv('DB_PASSWORD'),
    'attributes' => []
];

The vendor/craftcms/cms/src/config/DbConfig.php override that functioned previously is summarized here:

public function init()
{
    if ($this->dsn) {
        if (($pos = strpos($this->dsn, ':')) === false) {
            throw new InvalidConfigException('Invalid DSN: ' . $this->dsn);
        }
        $this->driver = substr($this->dsn, 0, $pos);
        $params = substr($this->dsn, $pos + 1);
        foreach (explode(';', $params) as $param) {
            if (($pos = strpos($param, '=')) === false) {
                throw new InvalidConfigException('Invalid DSN param: ' . $param . 'in - params: ' . $params);
            }
            $paramName = substr($param, 0, $pos);
            $paramValue = substr($param, $pos + 1);
            switch ($paramName) {
                case 'host':
                    $this->server = $paramValue;
                    break;
                case 'port':
                    $this->port = $paramValue;
                    break;
                case 'dbname':
                    $this->database = $paramValue;
                    break;
                case 'unix_socket':
                    $this->unixSocket = $paramValue;
                    break;
                case 'charset':
                    $this->charset = $paramValue;
                    break;
                case 'user': // PG only
                    $this->user = $paramValue;
                    break;
                case 'password': // PG only
                    $this->password = $paramValue;
                    break;
                case 'sslmode': // PG only
                    $this->sslmode = $paramValue;
                    break;
                case 'sslcert': // PG only
                    $this->sslcert = $paramValue;
                    break;
                case 'sslkey': // PG only
                    $this->sslkey = $paramValue;
                    break;
                case 'sslrootcert': // PG only
                    $this->sslrootcert = $paramValue;
                    break;

The .env file looks something like this:

CRAFT_ENVIRONMENT=production
LANG=C.UTF-8
REDIS_HOST=******
DB_DRIVER=pgsql
DB_DSN=pgsql:host=******;port=5432;dbname=******
DB_SERVER=******
DB_DATABASE=******
DB_PASSWORD=******
DB_SCHEMA=public
DB_PORT=5432
DB_USER=******
ENVIRONMENT=production
0

1 Answer 1

0

Obviously, we don't recommend anyone modify core Craft files, but I can't think of any recent changes around this on the Craft 3 branch (Craft 4 did make some changes).

I can't tell from your comment if it started to not work on 3.7.28, or 3.7.28 was the last one it worked on and it broke on 3.7.29 and later.

Assuming it's the latter, you can see a list of changed files between the releases that might help you narrow it down via https://github.com/craftcms/cms/compare/3.7.28...3.7.29

4
  • Thank you. Obviously I totally agree on modifying core Craft files. Is there a clean way to do this?
    – John
    May 13 at 1:43
  • Maybe I'm not following, but if you set the full DSN with all of your SSL settings directly in craftcms.com/docs/3.x/config/db-settings.html#dsn, then Craft should use it directly.
    – Brad Bell
    May 13 at 19:35
  • Thanks again for the response @BradBell. The first code snippet in my post is of the DSN I construct in config/db.php. Are you saying that what I have there should just work? It only seems to work with the hack in the second code snippet above, and that only when building with composer create-project craftcms/craft "1.1.6" and pinning Craft to version 3.7.38.
    – John
    May 13 at 20:52
  • "Are you saying that what I have there should just work?" - I think so, yeah. If this is reproducible on a public box and you can share some SSH credentials, shoot an email over to support@craftcms.com and we can poke around a bit.
    – Brad Bell
    2 days ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.