【问题标题】:Problems connecting codeigniter 4 with postgres - CodeIgniter\Database\Exceptions\DatabaseException #8将 codeigniter 4 与 postgres 连接时出现问题 - CodeIgniter\Database\Exceptions\DatabaseException #8
【发布时间】:2021-01-16 13:05:12
【问题描述】:

我想用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 em> 并显示 throw new DatabaseException('Unable to connect to the database.');

了解更多有关该主题的人可以帮助我,将不胜感激。

【问题讨论】:

  • 遇到同样的问题。你解决了吗?

标签: php database postgresql codeigniter codeigniter-4


【解决方案1】:

您的 .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

【讨论】:

  • 我只取消了带有设置的默认数据库组的注释并将 Postgre 部分大写,但它仍然标记我 CodeIgniter\Database\Exceptions\DatabaseException #8
  • 您能否在帖子中添加异常所说的内容?另外,您是否正在使用 spark 运行 codeigniter?如果是这样,请查看此论坛主题:forum.codeigniter.com/thread-77463-post-379847.html
  • 它向我发送 CodeIgniter\Database\Exceptions\DatabaseException #8 的消息并且异常 throw new DatabaseException('Unable to connect to the database.');我没有用spark,我应该配置一些spark?
  • 你作为服务器运行什么?
  • 我在本地工作,我尝试在 CodeIgniter 3 中运行,我收到 pg_connect () 错误:无法连接到 PostgreSQL 服务器:SCRAM 身份验证需要 libpq 版本 10 或更高版本,你知道吗?我认为错误来自这里,阅读我发现我必须更新我的 libpq 但我不知道该怎么做
【解决方案2】:

我使用了 codeigniter 4 和 postgres 10。

在 php 配置中,确保启用 pgsql 扩展

那么你可以使用这个 .env 设置

或者如果您想直接在 database.php 配置中进行编辑,请将 DSN 留空

【讨论】:

    【解决方案3】:

    对于 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 文件夹中,您也可以从浏览器运行。

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 2014-12-03
      • 2018-02-09
      相关资源
      最近更新 更多