-
kanagasabai Newbie

-
Posts: 3
Threads: 1
Joined: Apr 2024
Reputation:
0
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.
-
kanagasabai Newbie

-
Posts: 3
Threads: 1
Joined: Apr 2024
Reputation:
0
DB Config:
![[Image: Xv2pLES.jpeg]](https://i.imgur.com/Xv2pLES.jpeg)
Error message:
![[Image: Vx2NJxK.jpeg]](https://i.imgur.com/Vx2NJxK.jpeg)
currently changed the environment to development mode to capture the error,
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
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,5 +1,7 @@ <?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,7 +250,6 @@ class Connection extends BaseConnection return $str ? 'TRUE' : 'FALSE'; } - /** @psalm-suppress NoValue I don't know why ERROR. */ return parent::escape($str); } @@ -332,7 +337,7 @@ 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,7 +376,7 @@ class Connection extends BaseConnection /** * Returns an array of objects with Foreign key data * - * @return list<stdClass> + * @return array<string, stdClass> * * @throws DatabaseException */ @@ -496,7 +501,7 @@ 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 = ''; }
-
dedgun Newbie

-
Posts: 1
Threads: 0
Joined: May 2024
Reputation:
0
Same problem here with 4.5.1, downgraded with 4.4.8 and it worked.
-
kanagasabai Newbie

-
Posts: 3
Threads: 1
Joined: Apr 2024
Reputation:
0
(04-25-2024, 04:10 PM)kenjis Wrote: 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,5 +1,7 @@ <?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,7 +250,6 @@ class Connection extends BaseConnection return $str ? 'TRUE' : 'FALSE'; } - /** @psalm-suppress NoValue I don't know why ERROR. */ return parent::escape($str); } @@ -332,7 +337,7 @@ 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,7 +376,7 @@ class Connection extends BaseConnection /** * Returns an array of objects with Foreign key data * - * @return list<stdClass> + * @return array<string, stdClass> * * @throws DatabaseException */ @@ -496,7 +501,7 @@ 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 = ''; }
from the sample configuration for 'Postgre' given by you, i found it that changing the charset when configuring database connection from MySQL of 'utf8mb4' to PostgresQL 'utf8' did solved the problem.thank you.
-
Jag81 Junior Member
 
-
Posts: 15
Threads: 9
Joined: Dec 2021
Reputation:
1
I was having the same problem here today. I solved putting the port in the hostname. Like this:
public array $default = [
'DSN' => '',
'hostname' => 'localhost:8889',
...
The port configuration param seems to be ignored in any case.
|