I currently have a Drupal 7 site with a big database. I have to install the Drupal Commerce module and I would like to install the module using a separate database.

I know I have to setup the second database in the settings.php file, but I don't know the next steps I should follow.

How can I tell Drupal to use a different database for a module I am installing?

  • First of all why? What exactly are you trying to achieve in the end? And have you already heard of multisites? You should probably better provide a new multisite instance. – leymannx Oct 3 '19 at 19:32
  • What does 'install to another database' mean? Modules are registered as items of configuration. – Jaypan Oct 3 '19 at 21:07
  • Nope, i don't know about multisites. The reason why I want to have two databases is not to mix the commerce section with the the rest of the site. I fear that having the commerce tables in the same database may affect overall site performance. – Carlos Solé Oct 4 '19 at 12:42
  • Setting Drupal to use a different database for some tables is possible, but if the task is using a different database for all the tables used for a module, that is different. I would agree that a multi-site instance is probably the better solution. – kiamlaluno Oct 4 '19 at 19:30
  • The question seems clear, to me. It's possible to answer it with an answer that isn't too long, even if explaining how to setup a multi-site instance could use a link to Drupal documentation. – kiamlaluno Oct 4 '19 at 19:33

Drupal can be configured to use a different database for some of the tables. For example, with the following database settings, I would tell Drupal to use the db2 database for the users table.

$databases['default']['default'] = array(
  'driver' => 'mysql',
  'database' => 'db1',
  'username' => 'username',
  'password' => 'password',
  'host' => 'localhost',
  'prefix' => array(
    'users' => 'db2.',

$databases['default']['db2'] = array(
  'driver' => 'mysql',
  'database' => 'db2',
  'username' => 'username',
  'password' => 'password',
  'host' => 'localhost',
  'prefix' => '',

It doesn't actually work for PostgreSQL because a bug in Drupal core, and it works for those database engines for which using a different schema means using a different database.

Doing so for every table used for a module is more problematic, since the prefix must be explicitly set for all the tables used for that module, which means:

  • You need to know which tables are used for that module, including the tables Drupal creates for that module
  • If a new version of the module uses tables the previous version didn't use, the prefix must be explicitly set before updating the module
  • If a new version of Drupal uses tables for that module the previous version didn't use, the prefix must be explicitly set before updating Drupal
  • If you plan to install another module that store data related to that module, the prefix for the new tables must be explicitly set before installing the new module

Furthermore, for data related to that module that are stored in a table that contains data from all the installed modules, storing that data in a separate table only for a module is probably impossible.

Using two multi-site instances—Drupal installations that use the same code base, are accessible from different URLs (for example, https://example.net instead of https://example.org, or https://example.com/commerce instead of https://example.com), and use different databases (which could also use different database engines)—where one of the instances has the Drupal Commerce module installed, you would resolve the problem to put all the Drupal Commerce tables in a different database.

You would not need to set which database is used for the Drupal Commerce module, as that is already set for each multi-site instance. This means that any change in the tables used for the Drupal Commerce module (which are created from the module itself, Drupal core, or any third-party module) would not require changes to the Drupal database settings. Even updating the Drupal Commerce module or Drupal core would not require extra work to change the Drupal database settings. It would work for any database engine supported by Drupal or from third-party modules.

Since the multi-site instances don't normally share any data, in the same way two different Drupal sites don't share any data, if you want that users with an account on the multi-site instance without the Drupal Commerce module are able to access also the multi-site instance with that module without creating an account, or be logged-in when they already logged in into the first instance, you need to implement SSO on both the multi-site instances. There would be other data duplicated between the two multi-site instances, but those data would probably be less than all the data stored for the Drupal Commerce module.

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.