在 Mac OS X 上的 PHP 中启用 PostgreSQL 支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6588174/
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
Enabling PostgreSQL support in PHP on Mac OS X
提问by Jordan Scales
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.
我很难让命令“pg_connect()”在我的 Mac 上正常工作。我目前正在编写一个 PHP 脚本(从控制台执行)来读取 PostgreSQL 数据库并通过电子邮件发送报告。
I've gone into my php.ini
file and added
我已经进入我的php.ini
文件并添加了
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)
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): 图像未在 Unknown on line 0
PHP 致命错误: Call to undefined function pg_connect() in... (blah file here)
When running phpinfo()
, I see nothing about PostgreSQL, so what is my issue here?
运行时phpinfo()
,我看不到有关 PostgreSQL 的任何信息,那么我的问题是什么?
回答by Francois Deschenes
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:
OS X 附带的 PHP 版本不包括 PostgreSQL。您必须自己编译扩展。以下是一些说明:
- Find your version of PHP:
php -v
. - Download the version of PHP that matches yours:
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) - Extract the archive you downloaded:
tar -xzvf php-5.3.3.tar.gz
- Change to the PostgreSQL's extension directory:
cd php-5.3.3/ext/pgsql/
- Type
phpize
. - Type
./configure
. - Type
make
. - Type
sudo make install
. - Add the extension to you
php.ini
file by addingextension=pgsql.so
. (You may already have done this) - Restart Apache.
- 找到您的 PHP 版本:
php -v
. - 下载与您匹配的 PHP 版本:
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
- 切换到 PostgreSQL 的扩展目录:
cd php-5.3.3/ext/pgsql/
- 键入
phpize
。 - 键入
./configure
。 - 键入
make
。 - 键入
sudo make install
。 - 扩展程序添加到您
php.ini
通过添加文件extension=pgsql.so
。(你可能已经这样做了) - 重新启动阿帕奇。
Update for OS X Mountain LionApple has removed autoconf
from the newer versions of XCode so the procedure above will fail at #5. To solve that problem:
OS X Mountain Lionautoconf
的更新
Apple 已从较新版本的 XCode 中删除,因此上述过程将在 #5 处失败。为了解决这个问题:
- Type
/usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
. - Type
sudo chown -R $USER /usr/local/Cellar
. - Type
brew update
. - Type
brew install autoconf
.
- 键入
/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.
这应该安装autoconf
并允许您使用上述说明安装模块。
回答by Daniel Cukier
If you use home brew
, you can solve this with a command as simple as:
如果你使用 home brew
,你可以用一个简单的命令来解决这个问题:
brew install php55-pdo-pgsql
brew install php55-pdo-pgsql
for other php version, search with:
对于其他 php 版本,搜索:
brew search pgsql
brew search pgsql
回答by Jorge Casariego
This worked for me with OSX 10.9.4 ?Mavericks?
这对 OSX 10.9.4 有用吗?小牛队?
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:
下载 PHP 源代码。与 Mountain Lion 不同的是,您没有预先安装任何要链接的标头,因此需要将其放在 /usr/include/php 中。Mavericks 附带 PHP 5.4.17,但来自 php.net 的最新 5.4.x 源代码应该可以:
tar -jxvf php-5.4.20.tar.bz2
sudo mkdir -p /usr/include
sudo mv php-5.4.20 /usr/include/php
Configure PHP
配置 PHP
cd /usr/include/php
./configure --without-iconv
sudo cp /etc/php.ini.default /etc/php.ini
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:
我需要 pdo_pgsql 模块 - 假设您安装了必要的依赖项,相同的模式应该适用于几乎所有模块:
cd ext/pdo_pgsql
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
找不到自动配置。请检查您的 autoconf 安装和 $PHP_AUTOCONF 环境变量。然后,重新运行此脚本。错误:`phpize' 失败
So I had to use this command:
所以我不得不使用这个命令:
brew install autoconf
Then:
然后:
phpize
After that I tried to do: ./configure
之后我尝试做:./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
检查 pg_config... 未找到配置:错误:找不到 libpq-fe.h。请指定正确的PostgreSQL安装路径
So the solution was to specify correct PostgreSQL installation path:
所以解决方案是指定正确的PostgreSQL安装路径:
./configure --with-pdo-pgsql=/Library/PostgreSQL/9.3/
make
sudo make install
That copies pdo_pgsql.so to /usr/lib/php/extensions/no-debug-non-zts-20100525.
将 pdo_pgsql.so 复制到 /usr/lib/php/extensions/no-debug-non-zts-20100525。
Then simply add
然后简单地添加
extension=pdo_pgsql.so to /etc/php.ini
Run php -m
to confirm everything went to plan.
运行php -m
以确认一切按计划进行。
回答by ricardo
For those who installed php7/ngix/postgres with homebrew
对于那些用自制软件安装 php7/ngix/postgres 的人
You can install the PostgreSQL module with:
您可以使用以下命令安装 PostgreSQL 模块:
brew install php70-pdo-pgsql
After that, you have to restart the php service:
之后,您必须重新启动php服务:
brew services restart php70
回答by Christopher Charles Gorman
OS X El Capitan users can simply upgrade their version of PHP 5.6. This is a one liner that will do that.
OS X El Capitan 用户可以简单地升级他们的 PHP 5.6 版本。这是一个可以做到这一点的班轮。
curl -s http://php-osx.liip.ch/install.sh| bash -s 5.6
curl -s http://php-osx.liip.ch/install.sh| bash -s 5.6
回答by ThangTD
For php56
via brew:
对于php56
通过酿造:
brew install php56-pdo-pgsql
回答by ievgenii
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 in httpd.conf
by modifying your default LoadModule php5_module
path to LoadModule 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.
昨天升级后,我花了一整天试图让它在 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.so
. 只是决定将它留在这里作为那些升级他们的操作系统或只是 PHP 版本并面临同样问题的人的潜在解决方案。
回答by YP Leung
PostgreSQL by default is installed in a unusual place on MAC OS X:
默认情况下,PostgreSQL 安装在 MAC OS X 上一个不寻常的地方:
/Library/PostgreSQL/9.3
Given the location above you can type this:
鉴于上面的位置,您可以输入:
./configure --with-pgsql=/Library/PostgreSQL/9.3
回答by Majky
For those of you having openssl error while make
here is the solution
对于那些遇到 openssl 错误的人,make
这里是解决方案
OSX uses openssl 0.98 while installer is searching for 1.0.0
OSX 使用 openssl 0.98 而安装程序正在搜索 1.0.0
refer this link for instructions
请参阅此链接以获取说明
psycopg2 installation error - Library not loaded: libssl.dylib
回答by Net Dawg
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.
我下载了 Mac 版 PostgreSQL,并在安装后使用堆栈构建器来端到端地支持整个 EnterpriseDB Apache/PHP 堆栈。我提到这是一个可能的节省时间的选项,可能并不适合所有情况。如果 Mac OS X 附带的 apache 和 postgres 从未启动,应该可以正常工作。
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.
为了保持现有的 apache 托管应用程序(即 pre-PostgreSQL 安装旧版)稳定,我只会在端口 81 上安装较新的 EnterpriseDB apache(如果旧版 apache 实例已经在运行,stackbuilder 将提示输入新端口)。然后,使用 httpd.conf 中的 mod_proxy 为在端口 80 上运行的 apache 提供无缝的用户体验到托管在 PostgreSQL 上的应用程序。