postgresql 如何将 CakePHP 连接到 Postgres 数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20948599/
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 to connect CakePHP to a Postgres database?
提问by Luís de Sousa
I'm just starting with CakePHP, I went through the blog tutorial without trouble but would now like to try it with a proper DBMS like postgres. I tweaked the database.php file to point to a database I created in my local postgres instance:
我刚开始使用 CakePHP,我毫无困难地浏览了博客教程,但现在想用像 postgres 这样的合适的 DBMS 来尝试它。我调整了 database.php 文件以指向我在本地 postgres 实例中创建的数据库:
class DATABASE_CONFIG {
public $default = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'cakephp',
'password' => 'cakephp',
'database' => 'cakephp',
'schema' => 'blog_tuto',
'prefix' => '',
//'encoding' => 'utf8',
);
public $test = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'port' => '5432',
'login' => 'cakephp',
'password' => 'cakephp',
'database' => 'cakephp',
'schema' => 'blog_tuto',
'prefix' => '',
//'encoding' => 'utf8',
);
}
I have no issues connecting to this database with a SQL client, but CakePHP complains with the following message:
我使用 SQL 客户端连接到这个数据库没有问题,但 CakePHP 抱怨以下消息:
CakePHP is NOT able to connect to the database.
Database connection "Postgres" is missing, or could not be created.
Selected driver is not enabled
I already verified that the postgres DboSource class in present. The postgres php module is also installed:
我已经验证了当前的 postgres DboSource 类。还安装了 postgres php 模块:
$ php -m | grep pg
pdo_pgsql
pgsql
What am I missing? Thanks.
我错过了什么?谢谢。
Update I: The output of phpinfo related to Postgres goes below:
更新 I:与 Postgres 相关的 phpinfo 输出如下:
pdo_pgsql
PDO Driver for PostgreSQL enabled
PostgreSQL(libpq) Version 9.1.9
Module version 1.0.2
Revision $Id: pdo_pgsql.c 321634 2012-01-01 13:15:04Z felipe $
pgsql
PostgreSQL Support enabled
PostgreSQL(libpq) Version 9.1.9
Multibyte character support enabled
SSL support enabled
Active Persistent Links 0
Active Links 0
Directive Local Value Master Value
pgsql.allow_persistent On On
pgsql.auto_reset_persistent Off Off
pgsql.ignore_notice Off Off
pgsql.log_notice Off Off
pgsql.max_links Unlimited Unlimited
pgsql.max_persistent Unlimited Unlimited
Update II: When I accessed the application today trying to follow Ajir's suggestionI surprisingly got a different error message: "Authentication failed" - meaning that CakePHP is now able to connect. I installed Postgres 9.3 some months ago and had not re-created the tutorial objects; all I needed to do was run the SQL script and the tutorial application is now fully functional.
更新二:当我今天尝试按照Ajir 的建议访问应用程序时,我意外地收到了一条不同的错误消息:“身份验证失败”——这意味着 CakePHP 现在能够连接。几个月前我安装了 Postgres 9.3 并且没有重新创建教程对象;我需要做的就是运行 SQL 脚本,教程应用程序现在已完全正常运行。
Whatever is wrong is likely restricted to Postgres 9.1.
任何错误都可能仅限于 Postgres 9.1。
采纳答案by Luís de Sousa
This is likely an issue related to Postgres to 9.1, Cake PHP works perfectly fine with Postgres 9.3. Not a real solution, but a work around to fix this problem is:
这可能是与 Postgres 9.1 相关的问题,Cake PHP 与 Postgres 9.3 完美配合。不是真正的解决方案,但解决此问题的方法是:
Backup the database(s) hosted by Postgres 9.1.
Install Postgres 9.3 (eventually running in an alternative port).
Restore the database(s) into Postgres 9.3.
Point the
database.php
configuration to Postgres 9.3 (if it is running in an alternative port).Remove Postgres 9.1 (if not needed).
备份 Postgres 9.1 托管的数据库。
安装 Postgres 9.3(最终在替代端口中运行)。
将数据库恢复到 Postgres 9.3。
将
database.php
配置指向 Postgres 9.3(如果它在替代端口中运行)。删除 Postgres 9.1(如果不需要)。
回答by FAjir
This could resolve your problem :
这可以解决您的问题:
sudo apt-get install php5-pgsql
回答by Radekk
my configuration : Windows 7, IIS 7.5, php 5.3, postrgres 9.4, cakephp 2.6.3
我的配置:Windows 7、IIS 7.5、php 5.3、postrgres 9.4、cakephp 2.6.3
i've just added:
我刚刚补充说:
extension=php_pgsql.dll
extension=php_pdo_pgsql.dll
extension=php_pgsql.dll
extension=php_pdo_pgsql.dll
at the end of php.ini file after all extensions(php.ini copy of development). Remember that files must be presented C:\Program Files (x86)\PHP\v5.3\ext (this is my path to php installation)
在所有扩展名后的 php.ini 文件的末尾(开发的 php.ini 副本)。记住文件必须出现在C:\Program Files (x86)\PHP\v5.3\ext(这是我安装php的路径)
回答by PedroTanaka
please be sure that the schema that you are using is blog_tuto
and not public
which is the default in Postgresql. To test it, try to switch blog_tuto
with public
.
请确保您使用的架构是blog_tuto
而不是public
Postgresql 中的默认架构。为了测试它,尝试切换blog_tuto
使用public
。
And to avoid encoding problems, I'd suggest you to remove the comment of the encoding line and put there the encoding that you are using in your database (e.g. UTF-8, LATIN1);
为了避免编码问题,我建议您删除编码行的注释,并将您在数据库中使用的编码(例如 UTF-8、LATIN1)放在那里;