I am trying to add DB Information on the fly, instead of defining in settings.php

I am adding connection on module install

$other_database = array(
  'database' => 'pantheon',
  'username' => 'pantheon',
  'password' => 'password',
  'prefix' => '',
  'host' => 'dbserver.dfssg.drush.in',
  'port' => '4587',
  'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
  'driver' => 'mysql', 
Database::addConnectionInfo('legacy', 'default', $other_database);

and trying to use 'legacy' key with the migration file

  plugin: d8_entity
  key: legacy
  entity_type: node
  bundle: article

But, I getting an error

 The specified database connection is not defined: legacy

Is I am missing something?

  • 4
    When you add a connection “on-the-fly” it only persists for that request. So you have to add it again on each request, which isn’t conceptually compatible with doing it on module install. You might be able to hack something to work inside a module file, but really this information belongs in settings.php, or a file included from settings.php
    – Clive
    Mar 12, 2020 at 11:48
  • ohh, Thanks @Clive Mar 13, 2020 at 6:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.