如何在 Codeigniter 3 中连接到 POSTGRESQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39609511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to connect to POSTGRESQL in Codeigniter 3?
提问by Rajan
I am trying to connect to PostgreSQL using Codeigniter framework. Now in my database.php
我正在尝试使用 Codeigniter 框架连接到 PostgreSQL。现在在我的database.php
I have the following code :
我有以下代码:
$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
);
But When I run my site in localhost, I get following database error :
但是当我在 localhost 中运行我的网站时,我收到以下数据库错误:
A PHP Error was encountered
Severity: Warning
Message: pg_connect(): Unable to connect to PostgreSQL server: could not connect to server: Permission denied Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Permission denied Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
Filename: postgre/postgre_driver.php
Line Number: 154
遇到 PHP 错误
严重性:警告
消息:pg_connect():无法连接到 PostgreSQL 服务器:无法连接到服务器:权限被拒绝 服务器是否在主机“localhost”(::1) 上运行并接受端口 5432 上的 TCP/IP 连接?无法连接到服务器:权限被拒绝服务器是否在主机“localhost”(127.0.0.1)上运行并接受端口 5432 上的 TCP/IP 连接?
文件名:postgre/postgre_driver.php
行号:154
I tried putting this in my PostgreSQL.conf file :
我试着把它放在我的 PostgreSQL.conf 文件中:
listen_addresses = '*'
Where am I going wrong?
我哪里错了?
采纳答案by Abdulla Nilam
First enable Postgresql
extension in php.ini
首先启用Postgresql
扩展php.ini
extension=php_pgsql.dll
extension=php_pgsql.dll
You also can enable Postgresql
extension for PDO as well.
您也可以Postgresql
为 PDO启用扩展。
extension=php_pdo_pgsql.dll
extension=php_pdo_pgsql.dll
$db['default'] = array(
'port' => 5432, # Add
);
OR
或者
$db['default'] = array(
'dsn' => 'pgsql:host=localhost;port=5432;dbname=database_name',
'dbdriver' => 'pdo',
);
回答by Abdullah Al Shakib
First enable these two extensions
extension=php_pgsql.dll extension=php_pdo_pgsql.dll
- Then restart apache
- Add $db['default']['port'] = 5432in database.php file with all other codes.
首先启用这两个扩展
extension=php_pgsql.dll extension=php_pdo_pgsql.dll
- 然后重启apache
- 在 database.php 文件中添加$db['default']['port'] = 5432以及所有其他代码。
回答by Tudor
Tested with Codeigniter 4, PHP 7.3 and PostgreSQL 9.3.5:
使用 Codeigniter 4、PHP 7.3 和 PostgreSQL 9.3.5 进行测试:
1) Enable in your php.ini
1) 在你的 php.ini 中启用
extension=php_pgsql.dll
2) In app/Config/Database class override your $default property as follows:
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
];