I have a Drupal Environment that deploys to GitHub as well as my server. The only issue is I have local database credentials and server database credentials. For now when I do my merge/push, I have to manually change the settings file to make sure I don't connect to the server db and make local changes.

Is there a way to connect to a local database when appropriate and a server database any other time. I tried to use a different connection in the settings.local.php file, but it didn't seem to work.

  • I hope it's not a public repo.
    – leymannx
    Nov 3 '16 at 19:50
  • It's a private enterprise repository, soley used to track and push to the dev environment. Only reason I would ever have a settings.php file in a git repository would be to enable the push to a server. Nov 9 '16 at 19:59

in your settings.php, uncomment

# if (file_exists(__DIR__ . '/settings.local.php')) {
#   include __DIR__ . '/settings.local.php';
# }

so it looks like this:

if (file_exists(__DIR__ . '/settings.local.php')) {
  include __DIR__ . '/settings.local.php';

then in the same directory edit the settings.local.php to have your local settings.

Then add settings.local.php to your .gitignore file

That way, you can keep local settings that differ from the settings on dev.

So your dev server would have it's own unique settings.local.php file that differs from your local settings.local.php file.

If you need to check your database settings, you can add this at the bottom of settings.php

dump($databases); die();

You should see your correct settings (you can do this on both your local and your dev site to verify, then remove the dump command once you have verified.

enter image description here

  • I did exactly this and it didn't work. Nov 3 '16 at 18:28
  • 1
    I have updated my answer with some debugging info. Can you push that up to your dev site temporarily? See what's output?
    – oknate
    Nov 3 '16 at 19:25
  • Would be good to include the hash salt there as well to keep it out of the repo.
    – leymannx
    Nov 3 '16 at 19:52
  • 3
    This works fine. Make sure that you have no default database connection in the settings.php file or at least make sure that it is before the settings.local.php include, so that that file can override it and not the other way round
    – Berdir
    Nov 3 '16 at 22:46
  • @oknate - Got it working. Your instructions were fine. The mistake was on me. Nov 9 '16 at 20:00

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.