connecting postgresql and codeigniter

connecting postgresql and codeigniter

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Diagnosing Postgres Connection Failures in CodeIgniter

When transitioning from MySQL or SQLite to PostgreSQL, it is easy to assume that swapping the database name in your configuration file is sufficient. In your specific case, the error message "You have not selected a database type to connect to" points directly to a missing configuration property: dbdriver.

CodeIgniter relies on the $db['default']['dbdriver'] field to determine which native driver to instantiate. While some modern frameworks infer this from the DSN string, CodeIgniter's legacy Active Record implementation does not automatically parse the pgsql:// protocol prefix within your connection string to set the driver class internally without explicit instruction.

To resolve this, you must explicitly define the driver in your database configuration array. Update your $db['default'] configuration as follows:

$db['default'] = array(
    'dsn'   => '', // Optional: Can be omitted if using separate connection properties
    'hostname' => 'localhost',
    'username' => 'postgres',
    'password' => 'aPass',
    'database' => 'test',
    
    // This is the critical missing piece
    'dbdriver' => 'postgre', 
    
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    
    // Important: Postgres uses UTF8, not utf8_general_ci (which is MySQL specific)
    'char_set' => 'utf8',
    'dbcollat' => 'pg_catalog.UTF8', 
    
    'swap_pre' => '',
    'encrypt'  => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

The Driver Name Convention

A common pitfall when configuring PostgreSQL in CodeIgniter is the exact spelling of the driver string. In the framework's version history (specifically prior to the major overhaul in CI3.0+), the expected value was postgre. While some drivers accept generic names like mysql or sqlite, postgresql often fails because the internal class mapping expects the shortened alias postgre. If you use postgresql, the framework will not find the corresponding driver class, triggering the generic "database type not selected" error.

Character Set and Collation Mismatches

Your configuration snippet includes 'dbcollat' => 'utf8_general_ci'. This is a significant source of incompatibility between database engines. The suffix _ci denotes Case Insensitive collation, which is standard for MySQL. PostgreSQL does not use this nomenclature. While the connection might succeed with this setting in some loose configurations, it can lead to query errors or unexpected data sorting behavior because PostgreSQL expects a specific Postgres-compatible collation name, such as pg_catalog.UTF8 or en_US.utf8.

If you encounter issues with string comparison or ordering after fixing the driver, check your char_set and dbcollat values. Ensure they align with what the database server actually supports rather than copying settings from a MySQL configuration.

Verifying the DSN vs. Parameter Split

Your current setup mixes the DSN string (host=localhost;port=5432...) with individual parameters like hostname and username. In CodeIgniter 2.x, this hybrid approach often causes conflicts. The framework attempts to parse the DSN first; if it finds a port or database in the string but also sees explicit hostname keys, behavior can become unpredictable depending on the specific version of the framework loaded.

The most robust approach is to clear the dsn field entirely (set it to an empty string) and rely solely on the individual connection parameters. This ensures the framework treats the host, port, and credentials as distinct variables rather than trying to parse a complex string that might contain legacy MySQL syntax or unexpected characters.

Next Steps for Your Controller

Once the configuration array is corrected with dbdriver set to postgre, your controller method should function identically to how it was written. The get() method will execute against the PostgreSQL driver, and the result object returned by $feeds will contain the standard CodeIgniter database result class methods (like num_rows() or result_array()), allowing you to manipulate the data regardless of the underlying storage engine.

If you find yourself needing more advanced features like migrations or complex query building, consider evaluating modern alternatives. The Laravel framework, which powers https://laravelcompany.com, abstracts these connection nuances significantly better than CodeIgniter's legacy Active Record system. In Laravel, you define a config/database.php entry for your PostgreSQL database, and the Eloquent ORM handles the driver detection automatically without requiring you to manually specify 'postgre' or worry about collation strings in the config file.

For now, however, fixing the missing dbdriver property is the immediate requirement to get your CodeIgniter application communicating with your PostgreSQL instance.

Our network