如何让 php 与 postgresql 一起工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10640821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I enable php to work with postgresql?
提问by Aaron
<?php
try {
$dbh = new PDO('pgsql:host=localhost;port=5432;dbname=###;user=###;password=##');
echo "PDO connection object created";
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
I get the error message "Could Not Load Driver"
我收到错误消息“无法加载驱动程序”
回答by Jorge Olivares
You need to install the pgsql module for php. In debian/ubuntu is something like this:
您需要为 php 安装 pgsql 模块。在 debian/ubuntu 中是这样的:
sudo apt-get install php5-pgsql
Or if the package is installed, you need to enable de module in php.ini
或者如果安装了包,则需要在php.ini中启用de模块
extension=php_pgsql.dll (windows)
extension=php_pgsql.so (linux)
Greatings.
祝福。
回答by Sachin Puri
Try this:
尝试这个:
Uncomment the following in php.ini by removing the ";"
通过删除“;”在 php.ini 中取消注释以下内容
;extension=php_pgsql.dll
Use the following code to connect to a postgresql database server:
使用以下代码连接到 postgresql 数据库服务器:
pg_connect("host=localhost dbname=dbname user=username password=password")
or die("Can't connect to database".pg_last_error());
回答by Nadav B
For debian/ubuntu install
对于 debian/ubuntu 安装
sudo apt-get install php-pgsql
回答by Braian Coronel
- SO: Windows/Linux
- HTTP Web Server: Apache
- Programming language: PHP
- 所以:Windows/Linux
- HTTP 网络服务器:Apache
- 编程语言:PHP
Enable PHP to work with PostgreSQL in Apache
使 PHP 能够与 Apache 中的 PostgreSQL 一起使用
In Apache I edit the following configuration file: C:\xampp\php.ini
在 Apache 中,我编辑以下配置文件: C:\xampp\php.ini
I make sure to have the following lines uncommented:
我确保取消注释以下几行:
extension=php_pgsql.dll
extension=php_pdo_pgsql.dll
Finally restart Apache before attempting a new connection to the database engine.
最后在尝试与数据库引擎建立新连接之前重新启动 Apache。
Also, I leave my code that ensures that the connection is unique:
另外,我留下了确保连接唯一的代码:
private static $pdo = null;
public static function provideDataBaseInstance() {
if (self::$pdo == null) {
$dsn = "pgsql:host=" . HOST .
";port=5432;dbname=" . DATABASE .
";user=" . POSTGRESQL_USER .
";password=" . POSTGRESQL_PASSWORD;
try {
self::$pdo = new PDO($dsn);
} catch (PDOException $exception) {
$msg = $exception->getMessage();
echo $msg .
". Do not forget to enable in the web server the database
manager for php and in the database instance authorize the
ip of the server instance if they not in the same
instance.";
}
}
return self::$pdo;
}
GL
GL
回答by Wojciech Legierski
I have to add in httpd.conf this line (Windows):
我必须在 httpd.conf 中添加这一行(Windows):
LoadFile "C:/Program Files (x86)/PostgreSQL/8.3/bin/libpq.dll"
回答by augustowebd
just install the database driver:
只需安装数据库驱动程序:
apt-get install php5-pgsql php5-mysql php5-sqlite ... and so on ...
apt-get install php5-pgsql php5-mysql php5-sqlite ... 等等...
and be happy!
而且要快乐!
回答by Panagiotis Piperopoulos
I installed PHP on windows IIS using Windows Platform Installer (WPΙ). WPΙ creates a "PHP Manager" tool in "Internet Information Services (IIS) Manager" console. I am configuring PHP using this tool.
我使用 Windows 平台安装程序 (WPPI) 在 Windows IIS 上安装了 PHP。WPPI 在“Internet 信息服务 (IIS) 管理器”控制台中创建了一个“PHP 管理器”工具。我正在使用这个工具配置 PHP。
in http://php.net/manual/en/pdo.installation.phpsays:
在http://php.net/manual/en/pdo.installation.php 中说:
PDO and all the major drivers ship with PHP as shared extensions, and simply need to be activated by editing the php.ini file: extension=php_pdo.dll
PDO 和所有主要驱动程序都附带 PHP 作为共享扩展,只需通过编辑 php.ini 文件即可激活:extension=php_pdo.dll
so i activated the extension using PHP Manager and now PDO works fine
所以我使用 PHP Manager 激活了扩展,现在 PDO 工作正常
PHP manager simple added the following two lines in my php.ini, you can add the lines by hand. Of course you must restart the web server.
PHP manager simple在我的php.ini中添加了以下两行,你可以手动添加。当然,您必须重新启动 Web 服务器。
[PHP_PDO_PGSQL]
extension=php_pdo_pgsql.dll
[PHP_PDO_PGSQL]
扩展名=php_pdo_pgsql.dll
回答by user5699115
I and many other shave spent way too long on this - this needs a massive improvement. After spending hours, I finally copied php_pgsql.dll from php's ext directory to Apache24's root directory (wherever you installed it) and finally Apache was able to get the php/pg modules and dlls loaded.
我和许多其他人在这方面花了太长时间 - 这需要大量改进。花了几个小时后,我终于将 php_pgsql.dll 从 php 的 ext 目录复制到了 Apache24 的根目录(无论你安装在哪里),最后 Apache 能够加载 php/pg 模块和 dll。
回答by rinat.io
You need to isntall pdo_pgsql package
你需要安装 pdo_pgsql 包
回答by rtfmpliz
in my case there are 2 php.ini, I had to uncomment extension pdo_pgsql in both php.ini
就我而言,有2 个 php.ini,我不得不在 php.ini 中取消注释扩展 pdo_pgsql
- in phpfolder
- in apachefolder
- 在php文件夹中
- 在apache文件夹中
both inside in wampfolder
都在wamp文件夹中

