PHP PDO + PostgreSQL Connection Error: Could not find Driver

你说的曾经没有我的故事 提交于 2019-12-25 00:43:02

问题


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.


回答1:


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!


来源:https://stackoverflow.com/questions/50237023/php-pdo-postgresql-connection-error-could-not-find-driver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!