diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc
index 3c8d121..0439272 100644
--- a/core/includes/install.core.inc
+++ b/core/includes/install.core.inc
@@ -171,7 +171,9 @@ function install_state_defaults() {
     // TRUE when there are valid config directories.
     'config_verified' => FALSE,
     // TRUE when there is a valid database connection.
+    // TRUE if database is empty & ready to install
+    'database_ready' => FALSE,
     // TRUE when a valid settings.php exists (containing both database
     // connection information and config directory names).
     'settings_verified' => FALSE,
@@ -321,9 +323,7 @@ function install_begin_request(&$install_state) {
   $container
     ->register('string_translation', 'Drupal\Core\StringTranslation\TranslationManager')
     ->addArgument(new Reference('language_manager'));
-  $container
-    ->register('path.matcher', 'Drupal\Core\Path\PathMatcher')
-    ->addArgument(new Reference('config.factory'));
 
   \Drupal::setContainer($container);
 
@@ -332,6 +332,15 @@ function install_begin_request(&$install_state) {
   $install_state['database_verified'] = install_verify_database_settings();
   $install_state['settings_verified'] = $install_state['config_verified'] && $install_state['database_verified'];
 
+  // installing factory tables only after the database check
+  if ($install_state['database_verified'] && $install_state['database_ready']) {
+    $container
+      ->register('path.matcher', 'Drupal\Core\Path\PathMatcher')
+      ->addArgument(new Reference('config.factory'));
+  }
+
+  $install_state['settings_verified'] = $install_state['config_verified'] && $install_state['database_verified'];
+
   if ($install_state['settings_verified']) {
     try {
       $system_schema = system_schema();
@@ -679,6 +692,11 @@ function install_tasks($install_state) {
       'run' => $install_state['settings_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
       'function' => 'Drupal\Core\Installer\Form\SiteSettingsForm',
     ),
+    'install_database_ready' => array(
+      'run' => $install_state['database_ready'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
+
+    ),
     'install_base_system' => array(
       'run' => $install_state['base_system_verified'] ? INSTALL_TASK_SKIP : INSTALL_TASK_RUN_IF_NOT_COMPLETED,
     ),
@@ -1019,6 +1039,22 @@ function install_verify_database_settings() {
 }
 
 /**
+ * Check if database is ready (assuming no drupal previously installed)
+ * @param $install_state
+ */
+function install_database_ready($install_state) {
+  $system_schema = system_schema();
+  end($system_schema);
+  $table = key($system_schema);
+
+  if ($database = Database::getConnectionInfo()) {
+    if (Database::getConnection()->schema()->tableExists($table)) {
+      throw new AlreadyInstalledException(\Drupal::service('string_translation'));
+    }
+  }
+}
+
+/**
  * Checks a database connection and returns any errors.
  */
 function install_database_errors($database, $settings_file) {
