Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting postgresql and codeigniter

I'm new using postgresql and I've been using Codeigniter for a year.

I have a small postgresql database and I wanna call it from Codeigniter.

In my database.php file I have this setup:

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

$db['default'] = array(
'dsn'   => 'pgsql:host=localhost;port=5432;dbname=test;user=postgres;password=aPass',
// 'dsn'    => '',
'hostname' => 'localhost',
'username' => 'postgres',
'password' => 'aPass',
'database' => 'test',
'dbdriver' => '',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
  // 'port' => '5432'
);

In a controller I have this function:

public function dbtest(){

    $this->load->database();

    $feeds = $this->db->get('vts_feeds');
    echo $feeds;die();
}

The result I'm getting is:

An Error Was Encountered
You have not selected a database type to connect to.

Why is it not working?

like image 376
Limon Avatar asked Apr 14 '15 15:04

Limon


People also ask

How to connect CodeIgniter with PostgreSQL?

Create PostgreSQL Database connection By Codeigniter (both PDO & DB Driver example) Then restart the Apache server. Now by echo phpinfo() you will see pgsql block in PHP Configuration View. Ok lets go to Codeigniter config folder as usual and open the database.

How to connect PostgreSQL database in CodeIgniter 4?

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 !==

How do I connect to PostgreSQL and PHP?

Connecting to Database php $host = "host = 127.0. 0.1"; $port = "port = 5432"; $dbname = "dbname = testdb"; $credentials = "user = postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!

Is PostgreSQL compatible with PHP?

PHP provides many functions for working directly with PostgreSQL databases. To connect to PostgreSQL using native functions, follow these steps: Use the following PHP code to connect to PostgreSQL and select a database.


1 Answers

Try to set,

dbdriver - The database type. ie: mysql, postgres, odbc, etc. Must be specified in lower case.

More Info: https://www.codeigniter.com/user_guide/database/configuration.html

EDIT: Try this config for PDO in postgres

$db['default']['hostname'] = 'pgsql:host=localhost;dbname=yourdb'; //set host
$db['default']['username'] = 'your username'; //set username
$db['default']['password'] = 'your password'; //set password
$db['default']['database'] = 'your database'; //set databse
$db['default']['dbdriver'] = 'pdo'; //set driver here
like image 73
Always Sunny Avatar answered Sep 20 '22 20:09

Always Sunny