diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php
index 462699b..165eb22 100644
--- a/core/lib/Drupal/Core/Database/Connection.php
+++ b/core/lib/Drupal/Core/Database/Connection.php
@@ -741,6 +741,20 @@ public function schema() {
   }
 
   /**
+   * Escapes a database name string.
+   *
+   * Force all database names to be strictly alphanumeric-plus-underscore.
+   * For some database drivers, it may also wrap the database name in
+   * database-specific escape characters.
+   *
+   * @return
+   *   The sanitized database name string.
+   */
+  public function escapeDatabase($database) {
+    return preg_replace('/[^A-Za-z0-9_.]+/', '', $database);
+  }
+
+  /**
    * Escapes a table name string.
    *
    * Force all table names to be strictly alphanumeric-plus-underscore.
@@ -1088,6 +1102,16 @@ public function supportsTransactionalDDL() {
    */
   abstract public function databaseType();
 
+  /**
+   * Creates a database.
+   *
+   * In order to use this method, you must be connected without a database
+   * specified.
+   *
+   * @param string $database
+   *   Desired name of database.
+   */
+  abstract public function createDatabase($database);
 
   /**
    * Gets any special processing requirements for the condition operator.
diff --git a/core/lib/Drupal/Core/Database/ConnectionDatabaseDoesNotExistException.php b/core/lib/Drupal/Core/Database/ConnectionDatabaseDoesNotExistException.php
new file mode 100644
index 0000000..0bb3fb0
--- /dev/null
+++ b/core/lib/Drupal/Core/Database/ConnectionDatabaseDoesNotExistException.php
@@ -0,0 +1,14 @@
+<?php
+/**
+ * @file
+ * Definition of Drupal\Core\Database\ConnectionDatabaseDoesNotExistException
+ */
+
+namespace Drupal\Core\Database;
+
+use RuntimeException;
+
+/**
+ * Exception thrown if specified database is not found.
+ */
+class ConnectionDatabaseDoesNotExistException extends RuntimeException {}
diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php b/core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
index ae24176..e73d017 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\ConnectionDatabaseDoesNotExistException;
 use Drupal\Core\Database\TransactionCommitFailedException;
 use Drupal\Core\Database\DatabaseException;
 use Drupal\Core\Database\Connection as DatabaseConnection;
@@ -24,6 +25,11 @@
 class Connection extends DatabaseConnection {
 
   /**
+   * Error code for "Unknown database" error.
+   */
+  const DATABASE_NOT_FOUND = 1049;
+
+  /**
    * Flag to indicate if the cleanup function in __destruct() should run.
    *
    * @var boolean
@@ -47,7 +53,9 @@ public function __construct(array $connection_options = array()) {
       // Default to TCP connection on port 3306.
       $dsn = 'mysql:host=' . $connection_options['host'] . ';port=' . (empty($connection_options['port']) ? 3306 : $connection_options['port']);
     }
-    $dsn .= ';dbname=' . $connection_options['database'];
+    if (!empty($connection_options['database'])) {
+      $dsn .= ';dbname=' . $connection_options['database'];
+    }
     // Allow PDO options to be overridden.
     $connection_options += array(
       'pdo' => array(),
@@ -113,6 +121,25 @@ public function databaseType() {
     return 'mysql';
   }
 
+  /**
+   * Overrides \Drupal\Core\Database\Connection::createDatabase().
+   *
+   * @throws
+   */
+  public function createDatabase($database) {
+    // Escape the database name.
+    $database = Database::getConnection()->escapeDatabase($database);
+
+    try {
+      // Create the database and set it as active.
+      $this->exec("CREATE DATABASE $database");
+      $this->exec("USE $database");
+    }
+    catch (\Exception $e) {
+      throw new ConnectionDatabaseDoesNotExistException($e->getMessage());
+    }
+  }
+
   public function mapConditionOperator($operator) {
     // We don't want to override any of the defaults.
     return NULL;
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 4835103..4010c8e 100644
--- a/core/lib/Drupal/Core/Database/Driver/mysql/Install/Tasks.php
+++ b/core/lib/Drupal/Core/Database/Driver/mysql/Install/Tasks.php
@@ -8,6 +8,9 @@
 namespace Drupal\Core\Database\Driver\mysql\Install;
 
 use Drupal\Core\Database\Install\Tasks as InstallTasks;
+use Drupal\Core\Database\Database;
+use Drupal\Core\Database\Driver\mysql\Connection;
+use Drupal\Core\Database\ConnectionDatabaseDoesNotExistException;
 
 /**
  * Specifies installation tasks for MySQL and equivalent databases.
@@ -33,4 +36,49 @@ public function name() {
   public function minimumVersion() {
     return '5.0.15';
   }
+
+  /**
+   * Checking database connection.
+   */
+  protected function connect() {
+    try {
+      // This doesn't actually test the connection.
+      db_set_active();
+      // Now actually do a check.
+      Database::getConnection();
+      $this->pass('Drupal can CONNECT to the database ok.');
+    }
+    catch (\Exception $e) {
+      // Attempt to create the database if it is not found.
+      if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
+        // Remove the database string from connection info.
+        $connection_info = Database::getConnectionInfo();
+        $database = $connection_info['default']['database'];
+        unset($connection_info['default']['database']);
+
+        // In order to change the Database::$databaseInfo array, need to remove
+        // the active connection, then re-add it with the new info.
+        Database::removeConnection('default');
+        Database::addConnectionInfo('default', 'default', $connection_info['default']);
+
+        try {
+          // Now, attempt the connection again; if it's successful, attempt to
+          // create the database.
+          Database::getConnection()->createDatabase($database);
+        }
+        catch (ConnectionDatabaseDoesNotExistException $e) {
+          // Still no dice; probably a permission issue. Raise the error to the
+          // installer.
+          $this->fail(st('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
+        }
+      }
+      else {
+        // Database connection failed for some other reason than the database not
+        // existing.
+        $this->fail(st('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', array('%error' => $e->getMessage())));
+        return FALSE;
+      }
+    }
+    return TRUE;
+  }
 }
diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
index 91d95ff..7399cd0 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\ConnectionDatabaseDoesNotExistException;
 use Drupal\Core\Database\StatementInterface;
 
 use PDO;
@@ -26,6 +27,11 @@ class Connection extends DatabaseConnection {
    */
   const POSTGRESQL_NEXTID_LOCK = 1000;
 
+  /**
+   * Error code for "Unknown database" error.
+   */
+  const DATABASE_NOT_FOUND = 7;
+
   public function __construct(array $connection_options = array()) {
     // This driver defaults to transaction support, except if explicitly passed FALSE.
     $this->transactionSupport = !isset($connection_options['transactions']) || ($connection_options['transactions'] !== FALSE);
@@ -55,6 +61,7 @@ public function __construct(array $connection_options = array()) {
 
     $this->connectionOptions = $connection_options;
 
+    $connection_options['database'] = (!empty($connection_options['database']) ? $connection_options['database'] : 'template1');
     $dsn = 'pgsql:host=' . $connection_options['host'] . ' dbname=' . $connection_options['database'] . ' port=' . $connection_options['port'];
 
     // Allow PDO options to be overridden.
@@ -167,6 +174,24 @@ public function databaseType() {
     return 'pgsql';
   }
 
+  /**
+   * Overrides \Drupal\Core\Database\Connection::createDatabase().
+   *
+   * @throws
+   */
+  public function createDatabase($database) {
+    // Escape the database name.
+    $database = Database::getConnection()->escapeDatabase($database);
+
+    try {
+      // Create the database and set it as active.
+      $this->exec("CREATE DATABASE $database WITH TEMPLATE template0 ENCODING='utf8' LC_CTYPE='en_US.utf8' LC_COLLATE='en_US.utf8'");
+    }
+    catch (\Exception $e) {
+      throw new ConnectionDatabaseDoesNotExistException($e->getMessage());
+    }
+  }
+
   public function mapConditionOperator($operator) {
     static $specials = array(
       // In PostgreSQL, 'LIKE' is case-sensitive. For case-insensitive LIKE
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 6dc40a2..8ff2776 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Install/Tasks.php
@@ -9,6 +9,8 @@
 
 use Drupal\Core\Database\Database;
 use Drupal\Core\Database\Install\Tasks as InstallTasks;
+use Drupal\Core\Database\Driver\pgsql\Connection;
+use Drupal\Core\Database\ConnectionDatabaseDoesNotExistException;
 
 use Exception;
 
@@ -42,6 +44,61 @@ public function minimumVersion() {
   }
 
   /**
+   * Check if we can connect to the database.
+   */
+  protected function connect() {
+    try {
+      // This doesn't actually test the connection.
+      db_set_active();
+      // Now actually do a check.
+      Database::getConnection();
+      $this->pass('Drupal can CONNECT to the database ok.');
+    }
+    catch (Exception $e) {
+    // Attempt to create the database if it is not found.
+      if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
+        // Remove the database string from connection info.
+        $connection_info = Database::getConnectionInfo();
+        $database = $connection_info['default']['database'];
+        unset($connection_info['default']['database']);
+
+        // In order to change the Database::$databaseInfo array, need to remove
+        // the active connection, then re-add it with the new info.
+        Database::removeConnection('default');
+        Database::addConnectionInfo('default', 'default', $connection_info['default']);
+
+        try {
+          // Now, attempt the connection again; if it's successful, attempt to
+          // create the database.
+          Database::getConnection()->createDatabase($database);
+          Database::closeConnection();
+
+          // Now, restore the database config.
+          Database::removeConnection('default');
+          $connection_info['default']['database'] = $database;
+          Database::addConnectionInfo('default', 'default', $connection_info['default']);
+
+          // Check the database connection.
+          Database::getConnection();
+          $this->pass('Drupal can CONNECT to the database ok.');
+        }
+        catch (ConnectionDatabaseDoesNotExistException $e) {
+          // Still no dice; probably a permission issue. Raise the error to the
+          // installer.
+          $this->fail(st('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
+        }
+      }
+      else {
+        // Database connection failed for some other reason than the database not
+        // existing.
+        $this->fail(st('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', array('%error' => $e->getMessage())));
+        return FALSE;
+      }
+    }
+    return TRUE;
+  }
+
+  /**
    * Check encoding is UTF8.
    */
   protected function checkEncoding() {
