I installed php 7.2 and php 7.3 on Ubuntu 18.04, and created a php file to log on to and insert data into the Postgres database. My script responded “could not find driver.”
Research turned up that I have to edit the php.ini files and enable the extensions for Postgres and PDO. Below are all the extension= lines in the php.ini files. The closest I could find are the pdo lines shown below.
extension=pdo_pgsql extension=pgsql
So I edited the php.ini files and removed the semi-colons from each of those lines to enable them. Then I restarted Apache2:
sudo systemctl restart apache2
But I still get “could not find driver.”
This is the entire listing of extensions from the php.ini files. Which other extensions do I need to enable to allow PDO access to Postgres?
php.ini 7.3
;extension=bz2 ;extension=curl ;extension=fileinfo ;extension=gd2 ;extension=gettext ;extension=gmp ;extension=intl ;extension=imap ;extension=interbase ;extension=ldap ;extension=mbstring ;extension=exif ; Must be after mbstring as it depends on it ;extension=mysqli ;extension=oci8_12c ; Use with Oracle Database 12c Instant Client ;extension=odbc ;extension=openssl ;extension=pdo_firebird ;extension=pdo_mysql ;extension=pdo_oci ;extension=pdo_odbc extension=pdo_pgsql ;extension=pdo_sqlite extension=pgsql ;extension=shmop
php.ini 7.2
;extension=bz2 ;extension=curl ;extension=fileinfo ;extension=gd2 ;extension=gettext ;extension=gmp ;extension=intl ;extension=imap ;extension=interbase ;extension=ldap ;extension=mbstring ;extension=exif ; Must be after mbstring as it depends on it ;extension=mysqli ;extension=oci8_12c ; Use with Oracle Database 12c Instant Client ;extension=odbc ;extension=openssl ;extension=pdo_firebird ;extension=pdo_mysql ;extension=pdo_oci ;extension=pdo_odbc extension=pdo_pgsql ;extension=pdo_sqlite extension=pgsql ;extension=shmop
This is the php logon script (the logon credentials are replaced with dummy values here because I can’t reveal them):
<?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();
}
?>
Then I edited the two .ini files and added “.so” at the end of each of the two lines and restarted Apache2, but I still get the same error message.
Thanks for any ideas.
Advertisement
Answer
I think you’re missing the php-pgsql library, run the following commands and it should start working (keep the changes for the php.ini files):
sudo apt install php-pgsql sudo service apache2 reload
I do not have Ubuntu which means the library names could be different.
Hope it helps