簡體   English   中英

如何在 Codeigniter 3 中連接到 POSTGRESQL?

[英]How to connect to POSTGRESQL in Codeigniter 3?

我正在嘗試使用 Codeigniter 框架連接到 PostgreSQL。 現在在我的database.php

我有以下代碼:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => '',
    'database' => 'fmsdb',
    'dbdriver' => 'postgre',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

但是當我在本地主機中運行我的網站時,我收到以下數據庫錯誤:

遇到 PHP 錯誤

嚴重性:警告

消息:pg_connect():無法連接到 PostgreSQL 服務器:無法連接到服務器:權限被拒絕 服務器是否在主機“localhost”(::1) 上運行並接受端口 5432 上的 TCP/IP 連接? 無法連接到服務器:權限被拒絕 服務器是否在主機“localhost”(127.0.0.1) 上運行並接受端口 5432 上的 TCP/IP 連接?

文件名:postgre/postgre_driver.php

行號:154

我試着把它放在我的 PostgreSQL.conf 文件中:

listen_addresses = '*'

我哪里錯了?

首先在php.ini啟用Postgresql擴展

extension=php_pgsql.dll

您還可以為 PDO 啟用Postgresql擴展。

extension=php_pdo_pgsql.dll


$db['default'] = array(
    'port'   => 5432, # Add 
);

$db['default'] = array(
    'dsn'   => 'pgsql:host=localhost;port=5432;dbname=database_name', 
    'dbdriver' => 'pdo',
);

codeigniter.com 中的數據庫配置

使用 Codeigniter 4、PHP 7.3 和 PostgreSQL 9.3.5 進行測試

1) 在你的 php.ini 中啟用

extension=php_pgsql.dll

2) 在 app/Config/Database 類中覆蓋您的 $default 屬性,如下所示:

/**
 * The default database connection.
 *
 * @var array
 */

public $default = [
    'DSN'      => '',
    'hostname' => 'your_host',
    'username' => 'your-user',
    'password' => 'your-password',
    'database' => 'your-database',
    'DBDriver' => 'postgre',
    'DBPrefix' => '',
    'pConnect' => false,
    'DBDebug'  => (ENVIRONMENT !== 'production'),
    'cacheOn'  => false,
    'cacheDir' => '',
    'charset'  => 'utf8',
    'DBCollat' => 'utf8_general_ci',
    'swapPre'  => '',
    'encrypt'  => false,
    'compress' => false,
    'strictOn' => false,
    'failover' => [],
    'port'     => 5432, //the default port 
];
  1. 首先啟用這兩個擴展

    extension=php_pgsql.dll extension=php_pdo_pgsql.dll
  2. 然后重啟apache
  3. 在 database.php 文件中添加$db['default']['port'] = 5432以及所有其他代碼。

暫無
暫無

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

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