1

There are a few similar questions that I have read through and followed the advice but to no end, and only being fairly new to this, I figured now was a good time to stop 'trying' things in case I break anything any further.

I'm receiving the following error when trying to connect to my database via PDO:

Connection error: could not find driver

1. I've ensured that Apache is linked to my homebrew PHP.

    $ which php
    /usr/local/bin/php

2. Have uncommented the following in php.ini

extension=php_pdo_pgsql.dll

The pdo_pgsql module shows up in php_info

3. My database connection is:

    <?php
        class Database
        {
            public $conn;
            public function getConnection()
            {

                try {
                    $this->conn = new PDO("postgres://$user:$pass@$host:$port/$db_name");
                    print_r($this->conn);
                    $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $this->conn->exec("set names utf8");
                } catch (PDOException $exception) {
                    echo "Connection error: " . $exception->getMessage();
                    exit();
                }

                return $this->conn;
            }
        }

I've triple checked these details and they are correct (albeit ommitted). I can connect with the same URI via IntelliJ's Database connection Wizard

4. I’ve edited /usr/local/var/postgres/postgresql.conf to include:

#listen_addresses = '*' 

I'm still not having any luck and am looking at some guidance in this head scratcher.

2
  • The php command is actually separate from the Apache PHP module. You are also able to specify a different php.ini for Apache as well. php -r "phpinfo();" will give you a lot of information about your install. Creating a web page with phpinfo will also allow you to see your Apache module configuration. Do not keep this web page in production or on a web accessible server. It gives out way too much info. See this webpage: liquidweb.com/kb/how-to-check-php-modules-with-phpinfo Commented May 8, 2018 at 15:35
  • Hey Eric, Thanks for your response. I have checked that the module exists in phpinfo output as stated above and there is definitely an entry for pdo_pgsql (one column heading is PDO Driver for PostgreSQL). Commented May 9, 2018 at 2:02

1 Answer 1

3

As I see you are using Linux, but tried enable .dll library which is used for Windows machines. It makes sense to comment this line.

Make sure that you have pdo_pgsql module enabled:

# php -m | grep pdo_pgsql
pdo_pgsql

Install it if it is not

# yum install php-pgsql

Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:

  1. Install PostgreSQL (but I think you already have this installed and configured)

    # yum install postgresql-server postgresql-contrib
    
  2. Updated config /var/lib/pgsql/data/pg_hba.conf, changed from ident to md5

    host    all             all             127.0.0.1/32            ident
    host    all             all             ::1/128                 ident
    

    After

    host    all             all             127.0.0.1/32            md5
    host    all             all             ::1/128                 md5
    

    Restart postgresql service

    # service postgresql restart
    
  3. Install PHP and PDO connector

    # yum install php php-pgsql
    

Here is an example of PHP script I used to test connection:

<?php

//  Configure DB Parameters
$host = "localhost";
$dbname = "masterdb";
$dbuser = "automation";
$userpass = "fGmK4hvDZPB6fr6c";

$dsn = "pgsql:host=$host;port=5432;dbname=$dbname;user=$dbuser;password=$userpass";

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 $dbname database successfully!";
 echo "\n";
 }
}catch (PDOException $e){
 // report error message
 echo $e->getMessage();
}

And the output:

# php pdo_test.php
Connected to the masterdb database successfully!
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.