PHP PDO + PostgreSQL连接错误:找不到驱动程序
我已经阅读并遵循了一些类似的问题,但一直没有结束,而且对此还很陌生,我认为现在是时候停止尝试"事情了,以防万一我打破任何东西的好时机进一步.
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.
尝试通过PDO连接到我的数据库时收到以下错误:
Connection error: could not find driver
1.我确保将Apache链接到我的自制PHP.
$ which php
/usr/local/bin/php
2.在php.ini
2. Have uncommented the following in php.ini
extension=php_pdo_pgsql.dll
pdo_pgsql模块显示在php_info
3.我的数据库连接是:
<?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;
}
}
我对这些详细信息进行了三重检查,它们是正确的(尽管已省略).我可以通过IntelliJ的数据库连接向导连接相同的URI
4.我已经编辑 /usr/local/var/postgres/postgresql.conf
以包括:
#listen_addresses = '*'
我仍然没有运气,正在寻找有关此头刮刀的一些指导.
I'm still not having any luck and am looking at some guidance in this head scratcher.
如我所见,您正在使用Linux,但是尝试启用用于Windows计算机的.dll库.注释这一行很有意义.
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.
确保已启用pdo_pgsql模块:
Make sure that you have pdo_pgsql module enabled:
# php -m | grep pdo_pgsql
pdo_pgsql
如果没有安装,请安装
# yum install php-pgsql
这是步骤为了使PHP + PostgreSQL在干净的CentOS 7安装上能够正常运行,
Here is steps that I did to make PHP+PostgreSQL work on my clean CentOS 7 install:
-
安装PostgreSQL(但我认为您已经安装了PostgreSQL,并且 已配置)
Install PostgreSQL (but I think you already have this installed and configured)
# yum install postgresql-server postgresql-contrib
更新的配置/var/lib/pgsql/data/pg_hba.conf,从ident更改为md5
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
之后
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
重新启动Postgresql服务
Restart postgresql service
# service postgresql restart
安装PHP和PDO连接器
Install PHP and PDO connector
# yum install php php-pgsql
这是我用来测试连接的PHP脚本示例:
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();
}
输出:
# php pdo_test.php
Connected to the masterdb database successfully!