【问题标题】:Fatal error: Uncaught Error: Call to undefined function pg_connect()致命错误:未捕获错误:调用未定义函数 pg_connect()
【发布时间】:2025-12-09 19:35:01
【问题描述】:

从 ubuntu 服务器调用我的 php 脚本时出现此错误。我已经使用 apt get 安装了 php5-pgsql,但错误仍然存​​在。下面是我的源代码,请帮忙。

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$host = "localhost";
$port = "5432";
$user = "postgres";
$pass = "root";
$db = "quickbed";

$con = pg_connect("host=$host port=$port dbname=$db user=$user password=$pass") or die ("Could not connect to server\n");

$query = "select * from tbl_hosts, (select count(*) from tbl_hosts) as cnt order by registration_time";
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");

$rows['spaces']  = pg_fetch_all($rs);

echo json_encode($rows);
pg_close($con);

?>

【问题讨论】:

  • 您是否在您的ini文件中启用了该模块?
  • 查看phpinfo()函数的输出并搜索扩展。很可能加载失败。
  • 位于 /etc/php5/apache2 的 php.ini 文件没有这些模块,但我手动添加了它们,即 extension=php_pgsql.dll extension=php_pdo_pgsql.dll。但是,我已取消注释位于 /etc/php/7.0/apache2 中的 ini 文件中的那些模块
  • 您还需要启用此功能extension="pgsql.so"
  • @G.Joe 在 *nix 操作系统中,您必须使用 '.so' 文件(而不是 '.*dll')

标签: php apache ubuntu


【解决方案1】:

您必须安装 PostgreSQL 模块并启用它。 http://php.net/manual/en/pgsql.installation.php

【讨论】:

  • 感谢 Neodan,这就是我所缺少的; php7.0-pgsql
最近更新 更多