diff --git a/core/lib/Drupal/Core/Database/DatabaseInvalidNameException.php b/core/lib/Drupal/Core/Database/DatabaseInvalidNameException.php
new file mode 100644
index 0000000..a0a0fd0
--- /dev/null
+++ b/core/lib/Drupal/Core/Database/DatabaseInvalidNameException.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * @file
+ * Contains \Drupal\Core\Database\DatabaseInvalidNameException.
+ */
+
+namespace Drupal\Core\Database;
+
+/**
+ * Exception thrown if the specified database name is invalid.
+ *
+ * @see \Drupal\Core\Database\Connection::escapeDatabase().
+ */
+class DatabaseInvalidNameException extends \RuntimeException implements DatabaseException {}
diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
index e6c16bb..57a05c8 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
@@ -10,6 +10,7 @@
 use Drupal\Core\Database\DatabaseExceptionWrapper;
 
 use Drupal\Core\Database\Database;
+use Drupal\Core\Database\DatabaseInvalidNameException;
 use Drupal\Core\Database\DatabaseNotFoundException;
 use Drupal\Core\Database\TransactionCommitFailedException;
 use Drupal\Core\Database\DatabaseException;
@@ -170,10 +171,12 @@ public function databaseType() {
    *   The name of the database to create.
    *
    * @throws \Drupal\Core\Database\DatabaseNotFoundException
+   * @throws \Drupal\Core\Database\DatabaseInvalidNameException
    */
   public function createDatabase($database) {
-    // Escape the database name.
-    $database = Database::getConnection()->escapeDatabase($database);
+    if ($database != Database::getConnection()->escapeDatabase($database)) {
+      throw new DatabaseInvalidNameException();
+    }
 
     try {
       // Create the database and set it as active.
diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Install/Tasks.php b/core/lib/Drupal/Core/Database/Driver/mysql/Install/Tasks.php
index 19fbe5f..f2bdab3 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Install/Tasks.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Install/Tasks.php
@@ -83,10 +83,19 @@ protected function connect() {
           // create the database.
           Database::getConnection()->createDatabase($database);
         }
-        catch (DatabaseNotFoundException $e) {
-          // Still no dice; probably a permission issue. Raise the error to the
-          // installer.
-          $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
+        catch (\Exception $e) {
+          switch (get_class($e)) {
+            case 'Drupal\Core\Database\DatabaseInvalidNameException':
+              $this->fail(t('The database name contains illegal characters'));
+              break;
+
+            case 'Drupal\Core\Database\DatabaseNotFoundException':
+              $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
+              break;
+
+            default:
+              $this->fail(t('An unspecified database error occurred'));
+          }
         }
       }
       else {
diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
index ac0184b..5769a60 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
@@ -9,6 +9,7 @@
 
 use Drupal\Core\Database\Database;
 use Drupal\Core\Database\Connection as DatabaseConnection;
+use Drupal\Core\Database\DatabaseInvalidNameException;
 use Drupal\Core\Database\DatabaseNotFoundException;
 use Drupal\Core\Database\StatementInterface;
 use Drupal\Core\Database\IntegrityConstraintViolationException;
@@ -249,8 +250,9 @@ public function databaseType() {
    * @throws \Drupal\Core\Database\DatabaseNotFoundException
    */
   public function createDatabase($database) {
-    // Escape the database name.
-    $database = Database::getConnection()->escapeDatabase($database);
+    if ($database != Database::getConnection()->escapeDatabase($database)) {
+      throw new DatabaseInvalidNameException();
+    }
 
     // If the PECL intl extension is installed, use it to determine the proper
     // locale.  Otherwise, fall back to en_US.
diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php
index 83e0366..9739520 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php
@@ -93,10 +93,19 @@ protected function connect() {
           Database::getConnection();
           $this->pass('Drupal can CONNECT to the database ok.');
         }
-        catch (DatabaseNotFoundException $e) {
-          // Still no dice; probably a permission issue. Raise the error to the
-          // installer.
-          $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
+        catch (\Exception $e) {
+          switch (get_class($e)) {
+            case 'Drupal\Core\Database\DatabaseInvalidNameException':
+              $this->fail(t('The database name contains illegal characters'));
+              break;
+
+            case 'Drupal\Core\Database\DatabaseNotFoundException':
+              $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
+              break;
+
+            default:
+              $this->fail(t('An unspecified database error occurred'));
+          }
         }
       }
       else {
diff --git a/core/lib/Drupal/Core/Database/Install/Tasks.php b/core/lib/Drupal/Core/Database/Install/Tasks.php
index ee9c23e..d14deec 100644
--- a/core/lib/Drupal/Core/Database/Install/Tasks.php
+++ b/core/lib/Drupal/Core/Database/Install/Tasks.php
@@ -41,7 +41,7 @@
       'arguments'   => array(
         'CREATE TABLE {drupal_install_test} (id int NULL)',
         'Drupal can use CREATE TABLE database commands.',
-        'Failed to <strong>CREATE</strong> a test table on your database server with the command %query. The server reports the following message: %error.<p>Are you sure the configured username has the necessary permissions to create tables in the database?</p>',
+        'Failed to <strong>CREATE</strong> a test table on your database server with the command %query. The server reports the following message: %error.<p>Are you sure the configured username has the necessary permissions to create tables in the database and that the database name contains no illegal characters?</p>',
         TRUE,
       ),
     ),
