RTC222
RTC222

Reputation: 2323

php pdo for Postgres: "could not find driver"

I installed php on a new Ubuntu 18.04 server for Postgres, and I have a problem with php. Here are my php 7.3 installation steps:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php]
sudo systemctl restart apache2

After installing PHP 7.3, I installed pgsql.

sudo apt install php-pgsql
sudo service apache2 reload

Next I edited the php.ini file in /etc/php/7.3/apache2 and removed the semi-colons from the following lines:

extension=pdo_pgsql
extension=pgsql

I saved the file and did sudo systemctl restart apache2.

Finally I enabled the modules:

sudo phpenmod -v 7.3 pgsql
sudo phpenmod -v 7.3 pdo_pgsql
sudo systemctl restart apache2

Then I created a script to use pdo to log on to my Postgres database (the logon credentials are replaced with placeholder values here.)

<?php

$params = [
    'host' => '[IP Address]',
    'user' => '[username]',
    'pwd' => '[password]',
    'db' => '[dbname]'
];

$dsn = sprintf('pgsql:host=%s;dbname=%s;user=%s;password=%s',
    $params['host'],
    $params['db'],
    $params['user'],
    $params['pwd']);

try {
    $dsn = sprintf('pgsql:host=%s;dbname=%s;unix_socket=%s',
        $params['host'], $params['db'], $params['sock']);
    $opts = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
    $pdo = new PDO($dsn, $params['user'], $params['pwd'], $opts);
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (Throwable $e) {
    echo $e->getMessage();
}

?>

But the Firefox dev console echoes back: "could not find driver."

One clue is that php 7.3 is in /etc/apache2/mods-available, but it's not in /etc/apache2/mods-enabled, which suggests that it's not enabled. But when I try phpenmod -v 7.3 php7.3.conf, I get: WARNING: Module php7.3.conf ini file doesn't exist under /etc/php/7.3/mods-available.

I've done a lot of research on this, and those are the steps to follow. Much of that research was specific to MySQL, but that shouldn't matter for PDO.

Thanks for any ideas on why I am getting the message "could not find driver."

UPDATE:

I created a script to run phpinfo():

<?php
ob_start();
phpinfo();
$info = ob_get_clean();
echo $info;
?>

but it returns only html code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<style type="text/css">
body {background-color: #fff; color: #222; font-family: sans-serif;}
pre {margin: 0; font-family: monospace;}
a:link {color: #009; text-decoration: none; background-color: #fff;}
a:hover {text-decoration: underline;}
table {border-collapse: collapse; border: 0; width: 934px; box-shadow: 1px 2px 3px #ccc;}
.center {text-align: center;}
.center table {margin: 1em auto; text-align: left;}
.center th {text-align: center !important;}
td, th {border: 1px solid #666; font-size: 75%; vertical-align: baseline; padding: 4px 5px;}
h1 {font-size: 150%;}
h2 {font-size: 125%;}
.p {text-align: left;}
.e {background-color: #ccf; width: 300px; font-weight: bold;}
.h {background-color: #99c; font-weight: bold;}
.v {background-color: #ddd; max-width: 300px; overflow-x: auto; word-wrap:  break-word;}
.v i {color: #999;}
img {float: right; bo…
jquery.min.js line 2 > eval:12:21
?

But that's not what I expected.

Upvotes: 1

Views: 2509

Answers (1)

Will B.
Will B.

Reputation: 18416

As from our conversation, the issue was caused by having multiple versions of PHP installed and Apache was loading a different version of PHP (7.2) than the expected PHP 7.3.

To resolve the issue run the following commands:

sudo a2dismod php7.2
sudo a2enmod php7.3

This will disable php7.2 from being loaded by Apache and load php7.3 instead.


For your DSN (data source name), your current configuration is using a unix_socket which does not appear to be a valid option for the postgresql DSN. Additionally the $params['socket'] to be used for the unix_socket parameter is missing.

For more details see: https://www.php.net/manual/en/ref.pdo-pgsql.connection.php

If the database server is listening on the default port (5432) you can use:

try {
    $dsn = vsprintf('pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s', [
        'host' => '[IP Address]',
        'port' => '5432',
        'dbname' => '[dbname]',
        'user' => '[username]',
        'password' => '[password]',
    ]);
    $pdo = new PDO($dsn);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage();
} catch (Throwable $e) {
    echo $e->getMessage();
}

The DSN should result in: https://3v4l.org/aFKAW

pgsql:host=[IP Address];port=5432;dbname=[dbname];user=[username];password=[password]

Upvotes: 2

Related Questions