connecting postgresql and codeigniter

40,688

Solution 1

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

Solution 2

Remove the 'dsn' string and change to 'dbdriver' => 'postgre' in the config array.

Despite the CI docs stating the value should be 'postgres' if you check the directory and file names in system>database>drivers the folder is actually called 'postgre', the file name 'postgre_driver.php' and the class 'CI_DB_postgre_driver' so we can assume the docs are wrong here. Weird this hasnt raised its ugly head before

Solution 3

I just did it right now. You just leave :

$db['default']['dsn'] = '';

And set:

$db['default']['dbdriver'] = 'postgre';

Solution 4

In the file database.php where it reads:

'dbdriver' => ' ',

Put the following argument:

'dbdriver' => 'pgsql',
Share:
40,688
Limon
Author by

Limon

be better than yesterday

Updated on July 09, 2022

Comments

  • Limon
    Limon almost 2 years

    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?

  • Limon
    Limon about 9 years
    thx! I had to put pdo in order to work with the config I got. Do you know why does it not work if I put dbdriver = postgres, and just put dsn to blank?
  • Always Sunny
    Always Sunny about 9 years
    @Limon See my Edited answer, this might help you, Best of luck
  • Limon
    Limon about 9 years
    it says "invalid DB driver". I left dsn in blank and changed dbdriver to "postgres"
  • Limon
    Limon about 9 years
    the extension was already enabled. I have tried this way too but it's only working with dbdriver=> pdo and the dsn I mentioned
  • Mike Miller
    Mike Miller about 9 years
    Hmm strange. Do you have the driver file in the system database folder? This should just try to load the CI_postgres_db_driver file
  • Mike Miller
    Mike Miller about 9 years
    Looking at the file structure it looks like the right driver value is 'postgre' which is weird as the CI docs definitely say 'postgres'. Try without the 's' and see if that helps - I updated my answer
  • Limon
    Limon about 9 years
    yes, you are right. In CI docs there's a mistake, it is postgre and not postgres.
  • Limon
    Limon about 9 years
    I upgraded your answer but I had used first the solution given by @BunnySunny
  • M. Paul
    M. Paul over 3 years
    for me it says "Invalid DB driver". My OS is cent os.
  • dehart
    dehart about 3 years
    Please use the comment section for follow up questions and note that this question has already been answered