1

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.

12
  • 1
    Based on the fact you're seeing an error message for the driver, instead of the PHP code as text, Apache appears to be loading PHP. It sounds like you may have multiple versions of PHP installed and Apache has loaded a different version than 7.3. Suggest looking at How to check the presence of php and apache on ubuntu server through ssh to see if it helps. IE: a2enmod php7.3 Commented Jun 1, 2019 at 17:42
  • 1
    Remove the output buffering and only put <?php phpinfo(); exit; in an info.php and navigate to it in your browser. You would need to use a2dismod php7.2 as well to disable PHP 7.2 followed by a2enmod php7.3 to switch to php 7.3 in Apache. Commented Jun 1, 2019 at 17:46
  • 2
    Interesting conflict of php5 you'll need to do a2dismod php5 then. Commented Jun 1, 2019 at 17:51
  • 1
    Yea, your DSN has unix_socket in it and your params['socket'] is missing, can't help with that without knowing how your database server is configured. (Port or Socket) Commented Jun 1, 2019 at 18:02
  • 1
    Your DSN (database source name) is pgsql:host=%s;dbname=%s;unix_socket=%s See: php.net/manual/en/ref.pdo-pgsql.connection.php So you would to change it to read pgsql:host=%s;dbname=%s;user=%s;password=%s Commented Jun 1, 2019 at 18:05

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.