簡體   English   中英

將 codeigniter 4 與 postgres 連接時出現問題 - CodeIgniter\\Database\\Exceptions\\DatabaseException #8

[英]Problems connecting codeigniter 4 with postgres - CodeIgniter\Database\Exceptions\DatabaseException #8

我想用codeigniter 4和postgresql做一個項目我已經看過幾個例子,我什至閱讀了文檔,但是在連接到數據庫時仍然顯示錯誤。

我的文件 .env 是這樣的:

# database.tests.hostname = localhost
# database.tests.database = postgres_test
# database.tests.username = test
# database.tests.password = test123
# database.tests.DBDriver = postgre

我的文件 config\\App.php

public $tests = [
        'DSN'      => 'pgsql:host=localhost;port=5432;dbname=database_name',
        'hostname' => 'localhost',
        'username' => 'test',
        'password' => 'test123',
        'database' => 'postgres_test',
        'DBDriver' => 'postgre',
        'DBPrefix' => 'db_',  
        'pConnect' => false,
        'DBDebug'  => (ENVIRONMENT !== 'production'),
        'cacheOn'  => false,
        'cacheDir' => '',
        'charset'  => 'utf8',
        'DBCollat' => 'utf8_general_ci',
        'swapPre'  => '',
        'encrypt'  => false,
        'compress' => false,
        'strictOn' => false,
        'failover' => [],
        'port'     => 5433,
    ];

我不知道我在做什么錯,我知道 .env 文件覆蓋了 config\\App.php 配置,並給了我錯誤CodeIgniter\\Database\\Exceptions\\DatabaseException #8並顯示throw new DatabaseException('Unable連接到數據庫。');

對這個主題有更多了解的人可以幫助我,將不勝感激。

您的 .env 文件將覆蓋 config/App.php 設置。 但是,您正在設置評論項目的值。

#--------------------------------------------------------------------
# DATABASE
#--------------------------------------------------------------------

# database.default.hostname = localhost
# database.default.database = ci4
# database.default.username = root
# database.default.password = root
# database.default.DBDriver = MySQLi

# database.tests.hostname = localhost
# database.tests.database = ci4
# database.tests.username = root
# database.tests.password = root
# database.tests.DBDriver = MySQLi

所有以#開頭的值都被注釋掉了。 您需要將其刪除。 但是,就像您看到的那樣,有兩個數據庫組。 一個用於運行單元測試,另一個用於運行您的應用程序。 默認的一個運行你的應用程序。 這是您需要取消注釋的那個。 像這樣:

database.default.hostname = localhost
database.default.database = postgres_test
database.default.username = test
database.default.password = test123
database.default.DBDriver = Postgre

請注意,我以大寫形式編寫了 Postgre,這就是文檔中引用的方式。

有關此主題的更多信息,您可以在此處找到更多信息:

https://codeigniter.com/user_guide/database/configuration.html?highlight=env#configuring-with-env-file

我使用了 codeigniter 4 和 postgres 10。

在 php 配置中,確保啟用 pgsql 擴展

php.ini 配置

然后你可以使用這個 .env 設置

在此處輸入圖片說明

或者如果你想直接在database.php配置中編輯,將DSN留空

在此處輸入圖片說明

對於 PHP 7.4+,您是否嘗試在沒有 CI 的情況下連接到數據庫,以確保您正確安裝了數據庫?

你可以試試這個來檢查連接:

# test_postgre.php

$host = 'localhost';
$db = 'yourdbname';
$user = 'yourdbuser';
$pass = 'yourdbpass';
$port = '5432';

$db_handle = pg_connect("host={$host} port={$port} dbname={$db} user={$user} password={$pass}");

if ($db_handle) {
    echo "\nConnection attempt succeeded. \n\n";
} else {
    echo "\nConnection attempt failed. \n\n";
}

echo "Connection Information\n";
echo "======================\n\n";

echo "DATABASE NAME:" . pg_dbname($db_handle) . "\n";
echo "HOSTNAME: " . pg_host($db_handle) . "\n";
echo "PORT: " . pg_port($db_handle) . "\n\n";

然后轉到您的終端,然后運行:

php test_postgres.php

如果您可以將此文件放在您的public中,您也可以從瀏覽器運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM