Welcome Guest, Not a member yet? Register   Sign In
Codeigniter4 version 4.5.1 psql Database error "Unable to connect to the database".
#1

Hi All,
Im new to php ecosystem and currently working on fullstack web development especially in php codeigniter 4 framework.
we use postgresql for data storage and state managements, and the current ci4 version 4.4.7 is running in production for some time, after we updated to version 4.5.1,our app Postgres connection issue occured says "CodeIgniter\Database\Exceptions\DatabaseException,  Unable to connect to the database" but all the configuration are well tested and working while in version 4.4.7, in version 4.5.1 with same configuration it causes error. can you help me with this issue? thank you.
Reply
#2

(This post was last modified: 04-24-2024, 05:48 PM by kenjis.)

Show the DB Config and full error message.

Code:
$ php spark config:check Database
Reply
#3

DB Config:
[Image: Xv2pLES.jpeg]
Error message:
[Image: Vx2NJxK.jpeg]
currently changed the environment to development mode to capture the error,
Reply
#4

I don't know why the connection error occurs.

This is sample configuration for Postgre.
PHP Code:
        public array $default = [
            'DSN'        => '',
            'hostname'  => 'localhost',
            'username'  => 'root',
            'password'  => 'root',
            'database'  => 'ci4',
            'schema'    => 'public',
            'DBDriver'  => 'Postgre',
            'DBPrefix'  => '',
            'pConnect'  => false,
            'DBDebug'    => true,
            'charset'    => 'utf8',
            'swapPre'    => '',
            'failover'  => [],
            'port'      => 5432,
            'dateFormat' => [
                'date'    => 'Y-m-d',
                'datetime' => 'Y-m-d H:i:s',
                'time'    => 'H:i:s',
            ],
        ]; 

And this is the differences between v4.4.7 and v4.5.1.
PHP Code:
--- a/system/Database/Postgre/Connection.php
+++ b/system/Database/Postgre/Connection.php
@@ -1,+1,@@
 <?
php
 
+declare(strict_types=1);
+
 
/**
  * This file is part of CodeIgniter 4 framework.
  *
@@ -18,6 +20,7 @@ use ErrorException;
 use PgSql\Connection as PgSqlConnection;
 use PgSql\Result as PgSqlResult;
 use stdClass;
+use Stringable;
 
 /**
  * Connection for Postgre
@@ -231,12 +234,15 @@ class Connection extends BaseConnection
            $this->initialize();
        }
 
-        /** @psalm-suppress NoValue I don't know why ERROR. */
-        if (is_string($str) || (is_object($str) && method_exists($str'__toString'))) {
+
        if ($str instanceof Stringable) {
            if ($str instanceof RawSql) {
                return $str->__toString();
            }
 
+
            $str = (string) $str;
+
        }
+
+
        if (is_string($str)) {
            return pg_escape_literal($this->connID$str);
        }
 
@@ -
244,+250,@@ class Connection extends BaseConnection
            
return $str 'TRUE' 'FALSE';
        }
 
-
        /** @psalm-suppress NoValue I don't know why ERROR. */
        return parent::escape($str);
    }
 
@@ -
332,+337,@@ class Connection extends BaseConnection
    
/**
      * Returns an array of objects with index data
      *
-    * @return list<stdClass>
+    * @return array<string, stdClass>
      *
      * @throws DatabaseException
      */
@@ -356,10 +361,10 @@ class Connection extends BaseConnection
            $_fields    
explode(','preg_replace('/^.*\((.+?)\)$/''$1'trim($row->indexdef)));
            $obj->fields array_map(static fn ($v) => trim($v), $_fields);
 
-
            if (strpos($row->indexdef'CREATE UNIQUE INDEX pk') === 0) {
+
            if (str_starts_with($row->indexdef'CREATE UNIQUE INDEX pk')) {
                $obj->type 'PRIMARY';
            } else {
-
                $obj->type = (strpos($row->indexdef'CREATE UNIQUE') === 0) ? 'UNIQUE' 'INDEX';
+
                $obj->type = (str_starts_with($row->indexdef'CREATE UNIQUE')) ? 'UNIQUE' 'INDEX';
            }
 
            $retVal[$obj->name] = $obj;
@@ -
371,+376,@@ class Connection extends BaseConnection
    
/**
      * Returns an array of objects with Foreign key data
      *
-    * @return list<stdClass>
+    * @return array<string, stdClass>
      *
      * @throws DatabaseException
      */
@@ -496,+501,@@ class Connection extends BaseConnection
        
}
 
        // If UNIX sockets are used, we shouldn't set a port
-        if (strpos($this->hostname'/') !== false) {
+
        if (str_contains($this->hostname'/')) {
            $this->port '';
        
Reply




Theme © iAndrew 2016 - Forum software by © MyBB