ItGo.me Focus on IT Recommend

Home > postgresql - How to connect CakePHP to a Postgres database?

postgresql - How to connect CakePHP to a Postgres database?

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1074

【阿里云】双十一活动,全年抄底价,限时3天!(老用户也有),
入口地址https://www.aliyun.com/1111/home

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:

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:

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:

$ php -m | grep pg
pdo_pgsql
pgsql 

What am I missing? Thanks.

Update I : The output of phpinfo related to Postgres goes below:

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 suggestion I 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.

Whatever is wrong is likely restricted to Postgres 9.1.

postgresql cakephp
|
  this question
edited Dec 8 '14 at 9:13 asked Jan 6 '14 at 11:01 Luís de Sousa 888 1 13 30      Please check that the PostGres module has been loaded/activated in your web server. Or run phpinfo(); and check that you can see Postgres module has been loaded and is available. Kindly inform us of the result. –  Abdou Tahiri Jan 6 '14 at 11:40      I already verified that the postgres DboSource class in present You've done that on the cli, but the error message is from the webserver - they won't be using the same php.ini file. –  AD7six Jan 6 '14 at 11:47      AD7six, what client are you speaking of? As far I know all my code runs server side. –  Luís de Sousa Jan 6 '14 at 21:01      I am having the same problem, I am using the built-in Apache/PHP in Mac OS X Mavericks, I am also using Codeigniter in this same server, Codeigniter is able to connect to postgres without problem, I also made a postgres test script that is also being able to connect to postgres, I double checked my cakePHP database.php config file, what could I check for ? thanks --- public $default = array( 'datasource' => 'Database/Postgres', 'persistent' => false, 'host' => 'localhost', 'login' => 'cake', 'password' => 'cake', 'database' => 'cake', 'schema' => 'cake'); –  Nehemias Herrera Mar 18 '14 at 1:24

 | 

4 Answers
4

This could resolve your problem :

sudo apt-get install php5-pgsql

|
  this answer
answered Nov 24 '14 at 14:11 FAjir 1,022 1 12 24      I already have that package installed. –  Luís de Sousa Nov 24 '14 at 14:20 1   have you tried to check the CakePHP/Apache/Postgres logs ? –  FAjir Nov 28 '14 at 15:18

 |  ---Accepted---Accepted---Accepted---

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:

  1. Backup the database(s) hosted by Postgres 9.1.

  2. Install Postgres 9.3 (eventually running in an alternative port).

  3. Restore the database(s) into Postgres 9.3.

    Recommend:postgresql - Unable to connect to postgres database through heroku

    psql with the command: cse-190-fortune matanvardi$ psql "dbname=dccemfsa6camml host=ec2-54-235-152-226.compute-1.amazonaws.com \user=tfmfbqxqjznsbl password=********* port=5432 sslmode=require" I get this error: psql: could not connect to

  4. Point the database.php configuration to Postgres 9.3 (if it is running in an alternative port).

  5. Remove Postgres 9.1 (if not needed).


|
  this answer
answered Dec 8 '14 at 9:21 Luís de Sousa 888 1 13 30

 | 

my configuration : 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

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)


|
  this answer
answered Apr 21 '15 at 12:34 Radekk 31 4

 |  -1

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.

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);


|
  this answer
edited Dec 11 '14 at 0:00 answered Feb 6 '14 at 10:46 PedroTanaka 111 1 11

 | 

Recommend:postgresql - Migrating a local mysql database to heroku postgres from a cakephp application

. I know that in rails the solution is to install the postgres gem that will handle the migration, but I'm wondering more specifically about cakePHP. I've generated a sqldump of the mysql database using the solution here: http://book.cakeph

------splitte line----------------------------