How do I programmatically use an external database?
Setup the external database on settings.php
Select the correct database

$con = \Drupal\Core\DatabaseDatabase::getConnection('default','my_other_db');



I took the example from 
 http://www.drupal8.ovh/en/tutoriels/175/use-multiple-databases-drupal-8.