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