1

I am inserting records into a Postgres database table using PDO with named placeholders, but I get an error. I did not have PHP error logging turned on, so I enabled that, to go into php_errors.log (I created that new log file).

The new error log shows only two startup warnings:

[13-Aug-2019 17:26:08 UTC] PHP Warning:  PHP Startup: Unable to load dynamic library 'pdo_pgsql' (tried: /usr/lib/php/20180731/pdo_pgsql (/usr/lib/php/20180731/pdo_pgsql: cannot open shared object file: No such file or directory), /usr/lib/php/20180731/pdo_pgsql.so (/usr/lib/php/20180731/pdo_pgsql.so: undefined symbol: pdo_parse_params)) in Unknown on line 0

[13-Aug-2019 17:26:08 UTC] PHP Warning:  Module 'pgsql' already loaded in Unknown on line 0

The file /usr/lib/php/20180731/pdo_pgsql.so is there, despite what the warning says. In my php.ini file, the following lines are uncommented:

extension=pdo_pgsql
extension=pgsql

I thought that was all I need to do to enable pdo. However, in my phpinfo web page there is no entry for pdo, as there should be, so it looks like pdo is not there, even though pgsql works at least to log onto a Postgres database.

I do know that php is working with Postgres because when I run the script without the database insertion code (just logging on to Postgres), the dev console shows a success message (“successfully connected to database”).

My question is: what else do I need to do to enable pdo?

Here is the full script file, although it may not be needed for this question. As I said, without the try-catch block below, the dev console reports a successful logon to the Postgres database, but when I enable to try-catch block with the pdo record insertion code, it fails, apparently because pdo is not enabled.

<?php
echo 'Hello ' . htmlspecialchars($_POST["firstname"]) . '!' ;
$dsn = vsprintf('pgsql:host=%s;port=%s;dbname=%s;user=%s;password=%s',  [
    'host' => 'xx.xxx.xxx.xxx',
    'port' => '5432',
    'dbname' => 'dbname',
    'user' => 'username',
    'password' => 'password',
]);

echo $dsn;
echo PHP_EOL;

try{
 // create a PostgreSQL database connection
 $conn = new PDO($dsn);

 // display a message if connected to the PostgreSQL successfully
 if($conn){
 echo "Connected to the database successfully!";
 }
}catch (PDOException $e){
 // report error message
 echo $e->getMessage();
}

$fields = array('date', 'email', 'firstname', 'lastname', 'password',   'comments', 'sendupdates');
$fieldlist = implode(',', $fields);

echo $fieldlist;
echo PHP_EOL;

    $datefield = $_POST['datefield'];
    $email_field = $_POST['email_field'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $password = $_POST['password'];
    $comments = $_POST['comments'];
    $custom_checkbox = $_POST['custom_checkbox'];

echo $datefield;
echo PHP_EOL;
echo $email_field;
echo PHP_EOL;
echo $firstname;
echo PHP_EOL;

echo $lastname;
echo PHP_EOL;
echo $password;
echo PHP_EOL;
echo $comments;
echo PHP_EOL;
echo $custom_checkbox;
echo PHP_EOL;


try {

    $datefield = $_POST['datefield'];
    $email_field = $_POST['email_field'];
    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    $password = $_POST['password'];
    $comments = $_POST['comments'];
    $custom_checkbox = $_POST['custom_checkbox'];

    $data = [
        'date' => $datefield,
        'email' => $email_field,
        'firstname' => $firstname,
        'lastname' => $lastname,
        'password' => $password,
        'comments' => $password,
        'checkbox' => $custom_checkbox,
    ];

    $sql = "INSERT INTO psq01 (date, email, firstname, lastname, password, comments, sendupdates) VALUES (:date, :email, :firstname, :lastname, :password, :comments, :checkbox)";

    $stmt= $pdo->prepare($sql);
    $stmt->execute($data);

} catch (PDOException $e) {
    error_log($e->getMessage());
}

$conn = null;

?>

So again, what else do I need to enable PDO? I'm using Ubuntu 18.04.

Thanks for any help with this.

RTC222
  • 2,025
  • 1
  • 20
  • 53
  • Possible duplicate of [Install pdo for postgres Ubuntu](https://stackoverflow.com/questions/7180869/install-pdo-for-postgres-ubuntu) – aynber Aug 13 '19 at 18:15
  • 1
    That error is usually caused by using wrong versions of extensions. Every extension must be compiled against a compatible version of PHP that will load it. – Vahid Amiri Aug 13 '19 at 18:19
  • Not a repeat because I previously did apt-get install php-pgsql. I'll try it again. – RTC222 Aug 13 '19 at 18:19
  • For @Vahid Amiri -- the usual way is apt-get install php-pgsql -- is there some way to indicate version? I'm using PHP 7.3. – RTC222 Aug 13 '19 at 18:21
  • @RTC222 Ensure that the extension is compiled as 64bit binary if you are using 64bit PHP. Even though the error may seem about the wrong directory of extension, it's probably about other things. – Vahid Amiri Aug 13 '19 at 18:23
  • I just ran "a2enmod pgsql" and server replied "Module pgsql does not exist." I'll reinstall now. – RTC222 Aug 13 '19 at 18:26
  • Just ran sudo apt-get install php-pgsql and and server replied: php-pgsql is already the newest version (2:7.3+69+ubuntu18.04.1+deb.sury.org+2+php7.3) but I ran a2enmod pgsql and it replied again "Module pgsql does not exist." I just reinstalled it. What's going on? – RTC222 Aug 13 '19 at 18:27
  • Just ran sudo apt install php7.3-pgsql and that did install. However, a2enmod pgsql returns the same message. – RTC222 Aug 13 '19 at 18:32
  • Just ran phpenmod -v 7.3 pgsql and that worked. I'll see about pdo now. – RTC222 Aug 13 '19 at 18:41
  • Followup: I had previously edited the php.ini file in /etc/php/7.3/apache2. It turns out that I needed to edit the php.ini file in /etc/php/7.3/cli in order to enable the lines extension=pdo_pgsql and extension=pgsql. From my log file entries, it looks like that worked. – RTC222 Aug 13 '19 at 19:05

1 Answers1

1

You should try to comment back those extensions in php.ini file. It worked for me. Read more about in this post

I had the same difficulty. I am using Debian 10, PostgreSQL 12, Apache 2.4.38 and php7.3. Following the directions found on most sites:

1-I installed php

apt-get install php libapache2-mod-php

When trying to run my application I got Internal Server Error 500

2- I edited the php.ini file, uncommenting the extensions pdo_pgsql and pgsql and restarting the server

But the same error was repeated: Internal Server Error 500

3-I installed the pgsql package from php

apt-get install php7.3-pgsql
apachectl restart

After this step the error 500 was solved, but the two warnings mentioned by RTC222 appeared. Which by the way I only saw when I took a look at the php error.log file.

To solve the problem I did several searches that did not solve anything, until I found the aforementioned blog, which explains that the cause of this is (and I quote):

"There are two ways to load most extensions in PHP. One is by compiling the extension directly into the PHP binary. The other is by loading a shared extension dynamically via an ini file. The errors indicate that dynamic extensions are being loaded via .ini files, even though they are already compiled into the PHP binary. "

4- I uncommented the extensions of interest in the php.ini file and restarting the server everything was solved satisfactorily.

I hope this help someone.

Jorge
  • 61
  • 3
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 14 '21 at 20:41