在 Mac OS X 上的 PHP 中启用 PostgreSQL 支持
我很难让命令“pg_connect()”在我的 Mac 上正常工作。我目前正在编写一个 PHP 脚本(从控制台执行)来读取 PostgreSQL 数据库并通过电子邮件发送报告。
我已经进入我的 php.ini 文件并添加了
extension=pgsql.so
但是,我遇到了以下错误。
PHP 警告:PHP 启动:无法加载动态库 '/usr/lib/php/extensions/no-debug-non-zts-20090626/php_pgsql.so' - dlopen(/usr/lib/php/extensions/ no-debug-non-zts-20090626/php_pgsql.so, 9): 在第 0 行的未知中找不到图像
PHP 致命错误:调用...中未定义的函数 pg_connect()(此处为 blah 文件)
运行 phpinfo() 时,我看不到任何有关 PostgreSQL 的信息,那么我的问题是什么?
I'm having a terribly difficult time getting the command "pg_connect()" to work properly on my Mac. I'm currently writing a PHP script (to be executed from console) to read a PostgreSQL database and email a report.
I've gone into my php.ini
file and added
extension=pgsql.so
But, I'm met with the following error.
PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/extensions/no-debug-non-zts-20090626/php_pgsql.so' - dlopen(/usr/lib/php/extensions/no-debug-non-zts-20090626/php_pgsql.so, 9): image not found in Unknown on line 0
PHP Fatal error: Call to undefined function pg_connect() in... (blah file here)
When running phpinfo()
, I see nothing about PostgreSQL, so what is my issue here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
与 OS X 捆绑的 PHP 版本不包括 PostgreSQL。您必须自己编译扩展。以下是一些说明:
php -v
。curl -O http://us.php.net/distributions/php-5.3.3.tar.gz
。 (本示例下载 PHP 5.3.3,但这必须与您的版本匹配)tar -xzvf php-5.3.3.tar.gz
cd php-5.3.3/ext/pgsql/
phpize
。./configure
。make
。sudo make install
。extension=pgsql.so
将扩展添加到php.ini
文件中。 (您可能已经这样做了)OS X Mountain Lion 更新
Apple 已从较新版本的 XCode 中删除了
autoconf
,因此上述过程将在#5 处失败。要解决该问题:/usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew. rb)”
。brew update
。brew install autoconf
。这应该会安装
autoconf
并允许您使用上面的说明安装模块。The PHP version that comes bundled with OS X doesn't include PostgreSQL. You'll have to compile the extension yourself. Here are some instructions:
php -v
.curl -O http://us.php.net/distributions/php-5.3.3.tar.gz
. (This example downloads PHP 5.3.3 but this must match your version)tar -xzvf php-5.3.3.tar.gz
cd php-5.3.3/ext/pgsql/
phpize
../configure
.make
.sudo make install
.php.ini
file by addingextension=pgsql.so
. (You may already have done this)Update for OS X Mountain Lion
Apple has removed
autoconf
from the newer versions of XCode so the procedure above will fail at #5. To solve that problem:/usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
.sudo chown -R $USER /usr/local/Cellar
.brew update
.brew install autoconf
.That should install
autoconf
and allow you to install the module using the instructions above.如果您使用 home
brew
,您可以通过简单的命令解决此问题:brew install php55-pdo-pgsql
对于其他 php 版本,搜索:
brew搜索pgsql
If you use home
brew
, you can solve this with a command as simple as:brew install php55-pdo-pgsql
for other php version, search with:
brew search pgsql
这对我来说适用于 OSX 10.9.4 «Mavericks»
安装源
下载 PHP 源代码。与 Mountain Lion 不同,您没有预安装任何标头来链接,因此需要将其放在 /usr/include/php 中。 Mavericks 附带 PHP 5.4.17,但来自 php.net 的最新 5.4.x 源代码应该可以:
配置 PHP
构建模块
我需要 pdo_pgsql 模块 - 相同的模式应该适用于几乎任何模块,假设您安装了必要的依赖项:
在我的例子中,我遇到了以下错误:
找不到 autoconf。请检查您的 autoconf 安装和
$PHP_AUTOCONF 环境变量。然后,重新运行此脚本。错误:
`phpize' 失败
所以我不得不使用这个命令:
然后:
之后我尝试这样做:
./configure
但我遇到了下一个问题:
检查 pg_config...未找到配置:错误:找不到
libpq-fe.h。请指定正确的 PostgreSQL 安装路径
因此解决方案是指定正确的 PostgreSQL 安装路径:
将 pdo_pgsql.so 复制到 /usr/lib/php/extensions/no-debug-non-zts-20100525。
然后只需添加
Run
php -m
即可确认一切都按计划进行。This worked for me with OSX 10.9.4 «Mavericks»
Install sources
Download the PHP source code. Unlike on Mountain Lion, you don’t get any headers preinstalled to link against so need to put it in /usr/include/php. Mavericks ships with PHP 5.4.17, but the latest 5.4.x source from php.net should do:
Configure PHP
Building a module
I needed the pdo_pgsql module - the same pattern should apply to just about any module assuming you have the necessary dependencies installed:
In my case I had the following error:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script. ERROR:
`phpize' failed
So I had to use this command:
Then:
After that I tried to do:
./configure
but I had the next problem:
checking for pg_config... not found configure: error: Cannot find
libpq-fe.h. Please specify correct PostgreSQL installation path
So the solution was to specify correct PostgreSQL installation path:
That copies pdo_pgsql.so to /usr/lib/php/extensions/no-debug-non-zts-20100525.
Then simply add
Run
php -m
to confirm everything went to plan.对于那些使用自制程序安装 php7/ngix/postgres 的人,
您可以使用以下命令安装 PostgreSQL 模块:
之后,您必须重新启动 php 服务:
For those who installed php7/ngix/postgres with homebrew
You can install the PostgreSQL module with:
After that, you have to restart the php service:
OS X El Capitan 用户只需升级 PHP 5.6 版本即可。这是一款可以做到这一点的单衬。
卷曲 -s http://php-osx.liip.ch/install.sh | bash -s 5.6
OS X El Capitan users can simply upgrade their version of PHP 5.6. This is a one liner that will do that.
curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6
对于通过brew的
php56
:For
php56
via brew:默认情况下,PostgreSQL 安装在 MAC OS X 上的一个不寻常的位置:
根据上面的位置,您可以键入以下内容:
PostgreSQL by default is installed in a unusual place on MAC OS X:
Given the location above you can type this:
昨天升级后,我花了一整天的时间试图让它在 El Capitan 上工作,结果发现我忘记修改
httpd.conf
并更改默认 php 模块(版本 5.5)的路径.27)到我安装的(版本5.6.14)。这应该在 httpd.conf 中通过将默认的 LoadModule php5_module 路径修改为 LoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5 来完成。所以。只是决定将其留在这里,作为那些升级操作系统或仅升级 PHP 版本并面临相同问题的人的潜在解决方案。
I killed the whole day trying to make it work on El Capitan after I made an upgrade yesterday and it turned out that I forgot to modify
httpd.conf
and change the path from the default php module (version 5.5.27) to the one I installed (version 5.6.14). This should be done inhttpd.conf
by modifying your defaultLoadModule php5_module
path toLoadModule php5_module /usr/local/opt/php56/libexec/apache2/libphp5.so
.Just decided to leave it here as the potential solution for those who upgrade their OS or just the PHP version and face the same problem.
对于那些在
make
时遇到 openssl 错误的人,这里是解决方案OSX 在安装程序搜索 1.0.0 时使用 openssl 0.98,
请参阅此链接以获取说明
psycopg2 安装错误 - 库未加载: libssl.dylib
For those of you having openssl error while
make
here is the solutionOSX uses openssl 0.98 while installer is searching for 1.0.0
refer this link for instructions
psycopg2 installation error - Library not loaded: libssl.dylib
我下载了 Mac 版 PostgreSQL,并在安装后使用堆栈生成器来端到端地支持整个 EnterpriseDB Apache/PHP 堆栈。我提到这是一个可能节省时间的选项,可能并不适合所有情况。如果 Mac OS X 附带的 apache 和 postgres 从未启动过,应该可以正常工作。
为了保持现有的 apache 托管应用程序(即 PostgreSQL 安装之前的旧版)稳定,我只需在端口 81 上安装较新的 EnterpriseDB apache(如果旧版 apache 实例已在运行,stackbuilder 将提示新端口)。然后,对在端口 80 上运行的 apache 使用 httpd.conf 中的 mod_proxy,为 PostgreSQL 上托管的应用程序提供无缝的用户体验。
I downloaded PostgreSQL for Mac, and used the stack builder after installation to standup the entire EnterpriseDB Apache/PHP stack end-to-end. I mention this as a possible time saving option, probably not ideal for all situations. Should work OK if the apache and postgres shipped with Mac OS X were never started.
To keep existing apache hosted applications (i.e. pre-PostgreSQL install legacy) stable, I would just install the newer EnterpriseDB apache on port 81 (stackbuilder will prompt for new port if legacy apache instance is already running). Then, use mod_proxy in httpd.conf for the apache running on port 80 to provide seamless user experience to applications hosted on PostgreSQL.