0

I am working with Drupal 7.3.4 and attempting to follow the instructions of changing settings.php so that I can switch to a pgsql DB within a module and then switch back to the default mysql after doing a query.

I have set up in the settings.php file with the following format:

$databases = array (
  'default' => 
  array (
    'default' => 
    array (
      'database' => '',
      'username' => '',
      'password' => '',
      'host' => 'localhost',
      'port' => '',
      'driver' => 'mysql',
      'prefix' => '',
    ),
  ),
  'custom_db' =>
  array (
    'default' =>
    array (
      'database' => 'custom_db',
      'username' => 'dbuser',
      'password' => 'pass',
      'host' => 'urltoservice',
      'port' => '5432',
      'driver' => 'pgsql',
      'prefix' => '',
    ),
  ),
);

Now I am trying the process of switching to this DB in a module and verifying that the driver is changing.

db_set_active('custom_db');
//Get the currently active DB driver
drupal_set_message(db_driver());
db_set_active();

But, when I print the driver it is not switching to pgsql as expected. I am pretty sure it is still pointing to the default mysql db. Any ideas of what I may be doing wrong here would be greatly appreciated.

Thanks, Derek

  • try outputting the return value from the second call to db_set_active - it should return the current DB (which at that point would be custom_db) to confirm. Also, as always with Drupal, did you clear the cache? – Geoff Mar 8 '15 at 23:10
0

first check whether you are executing your query before doing db_set_active(); because this function call will set your db to default drupal db.

so you should go like this:

db_set_active('custom_db');

// execute your query related stuff here

then at last switch to your default db by db_set_active();

  • Thanks for this tip. I verified that I am executing my query to PGSQL before doing db_set_active(). – geo derek Mar 13 '15 at 14:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.