简体   繁体   English

将 codeigniter 4 与 postgres 连接时出现问题 - CodeIgniter\\Database\\Exceptions\\DatabaseException #8

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

I want to make a project with codeigniter 4 and postgresql I have already seen several examples, I even read the documentation but it still shows an error when connecting to the database.我想用codeigniter 4和postgresql做一个项目我已经看过几个例子,我什至阅读了文档,但是在连接到数据库时仍然显示错误。

My file .env is like this:我的文件 .env 是这样的:

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

And my file config\\App.php我的文件 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,
    ];

I don't know what I'm doing wrong, I understand that the .env file overwrites the config\\App.php configuration, and gives me the error CodeIgniter\\Database\\Exceptions\\DatabaseException #8 and show throw new DatabaseException('Unable to connect to the database.');我不知道我在做什么错,我知道 .env 文件覆盖了 config\\App.php 配置,并给了我错误CodeIgniter\\Database\\Exceptions\\DatabaseException #8并显示throw new DatabaseException('Unable连接到数据库。');

Someone who knows more about the subject who can help me would greatly appreciate it.对这个主题有更多了解的人可以帮助我,将不胜感激。

Your .env file will overwrite the config/App.php settings.您的 .env 文件将覆盖 config/App.php 设置。 However you are setting the values on commented items.但是,您正在设置评论项目的值。

#--------------------------------------------------------------------
# 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

All the values that start with # are commented.所有以#开头的值都被注释掉了。 You need to remove it.您需要将其删除。 However like you see there are two database groups.但是,就像您看到的那样,有两个数据库组。 One for running unit test the other to run your app.一个用于运行单元测试,另一个用于运行您的应用程序。 The default one runs your app.默认的一个运行你的应用程序。 That's the one you need to uncomment.这是您需要取消注释的那个。 Like so:像这样:

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

Note that I wrote the Postgre in uppercase as is that is how is referenced in the docs.请注意,我以大写形式编写了 Postgre,这就是文档中引用的方式。

For more information on this subject you can find more here:有关此主题的更多信息,您可以在此处找到更多信息:

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

I used codeigniter 4 and postgres 10.我使用了 codeigniter 4 和 postgres 10。

In the php configuration, make sure you enable the pgsql extension在 php 配置中,确保启用 pgsql 扩展

php.ini 配置

then you can use this .env setting然后你可以使用这个 .env 设置

在此处输入图片说明

or if you want to edit directly in the database.php configuration, leave the DSN blank或者如果你想直接在database.php配置中编辑,将DSN留空

在此处输入图片说明

For PHP 7.4+, did you try to connect to database without CI, to make sure you installed the database correctly?对于 PHP 7.4+,您是否尝试在没有 CI 的情况下连接到数据库,以确保您正确安装了数据库?

You can try this to check the connection:你可以试试这个来检查连接:

# 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";

Then go to your terminal, and run:然后转到您的终端,然后运行:

php test_postgres.php

If you can put this file in your public folder, you can run from your browser also.如果您可以将此文件放在您的public中,您也可以从浏览器运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM