How to set PDO sslmode?
How to set PDO sslmode?

Hi!
I use datatables/editor with an external managed postgresql database. This database requires that i set sslmode to "require".
Usually i connect to the database like this:
$DATABASE_CONNECTION = new PDO('pgsql:host='.$SERVER_HOST.';port='.$SERVER_PORT.';dbname='.$DB_NAME.';user='.$SERVER_USER.';password='.$SERVER_PASSWORD.';sslmode='.$SSLMODE);
$DATABASE_CONNECTION->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$DATABASE_CONNECTION->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
But i don't know how to set it with datatables/editor, because the $sql_details in the config file has no attribute 'sslmode' to set.
Should i set it somewhere in "pdoAttr"-array?
I tried to just add another attribute "sslmode" to the $sql_details-array.
Then i modified the connect function in PostgresQuery.php like that:
static function connect( $user, $pass='', $host='', $port='', $db='', $dsn='', $sslmode='' )
{
if ( is_array( $user ) ) {
$opts = $user;
$user = $opts['user'];
$pass = $opts['pass'];
$port = $opts['port'];
$host = $opts['host'];
$db = $opts['db'];
$dsn = isset( $opts['dsn'] ) ? $opts['dsn'] : '';
$pdoAttr = isset( $opts['pdoAttr'] ) ? $opts['pdoAttr'] : array();
}
if ( $port !== "" ) {
$port = "port={$port};";
}
try {
$pdoAttr[ PDO::ATTR_ERRMODE ] = PDO::ERRMODE_EXCEPTION;
$pdo = @new PDO(
"pgsql:host={$host};{$port}dbname={$db}".self::dsnPostfix( $dsn ),
$user,
$pass,
$sslmode,
$pdoAttr
);
} catch (\PDOException $e) {
// If we can't establish a DB connection then we return a DataTables
// error.
echo json_encode( array(
"error" => "An error occurred while connecting to the database ".
"'{$db}'. The error reported by the server was: ".$e->getMessage()
) );
exit(0);
}
return $pdo;
}
But then it tells me:
<b>Fatal error</b>: Uncaught ArgumentCountError: PDO::__construct() expects at most 4 parameters, 5 given in lib/Database/Driver/PostgresQuery.php:64
Line 64 is here line 26: $pdoAttr
I have not found where i can change the PDO-constructor parameters. Is this possible somehow?
Comming from c# - i'm not too familiar with php and databases (maybe as you can see), so any help would be much appreciated!
Replies
According to stackoverflow, you can put sslmode=require into your DSN string.
https://stackoverflow.com/questions/46852880/connecting-to-pgsql-over-ssl-via-php-pdo
Thank you, that helped! I've seen that link before, but was not sure where and how to insert these informations.
This is what worked: