diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php
index 2aa6315..0a94bc0 100644
--- a/core/lib/Drupal/Core/Database/Connection.php
+++ b/core/lib/Drupal/Core/Database/Connection.php
@@ -6,6 +6,7 @@
  */
 
 namespace Drupal\Core\Database;
+use GuzzleHttp\Psr7\Uri;
 
 /**
  * Base Database API class.
@@ -1433,4 +1434,73 @@ public function __sleep() {
     throw new \LogicException('The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution.');
   }
 
+  /**
+   * A helper function to convert a URI into connection options.
+   *
+   * This method can be overridden in order to customise how a URI is converted.
+   *
+   * @internal
+   *   This method should not be called. Use
+   *   \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo() instead.
+   *
+   * @param \GuzzleHttp\Psr7\Uri $uri
+   *   The URI to be converted to a connection array.
+   * @param string $root
+   *   The root directory of the Drupal installation.
+   * @param array $connection_options
+   *   The connection options.
+   *
+   * @return array
+   *   The connection options.
+   *
+   * @see \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
+   */
+  public static function convertDbUrlToConnectionInfoHelper(Uri $uri, $root, array $connection_options) {
+    $port = $uri->getPort();
+    if (!empty($port)) {
+      $connection_options['port'] = $port;
+    }
+    $user_info = explode(':', $uri->getUserInfo(), 2);
+    $connection_options['username'] = $user_info[0];
+    $connection_options['password'] = isset($user_info[1]) ? $user_info[1] : '';
+
+    return $connection_options;
+  }
+
+  /**
+   * A helper function to convert connection options into a URI.
+   *
+   * This method can be overridden in order to customise how connection options
+   * are converted.
+   * 
+   * @internal
+   *   This method should not be called. Use
+   *   \Drupal\Core\Database\Database::getConnectionInfoAsUrl() instead.
+   *
+   * @param array $connection_options
+   *   The array of connection options for a database connection.
+   * @param \GuzzleHttp\Psr7\Uri $uri
+   *   The URI to connect to the database.
+   *
+   * @return $this|\GuzzleHttp\Psr7\Uri
+   *   The URI to connect to the database.
+   * 
+   * @see \Drupal\Core\Database\Database::getConnectionInfoAsUrl()
+   */
+  public static function getConnectionInfoAsUrlHelper(array $connection_options, Uri $uri) {
+    $user = '';
+    if ($connection_options['username']) {
+      $user = $connection_options['username'];
+    }
+    $password = '';
+    if ($connection_options['password']) {
+      $password = $connection_options['password'];
+    }
+    $uri = $uri->withUserInfo($user, $password);
+    if (!empty($connection_options['port'])) {
+      $uri = $uri->withPort($connection_options['port']);
+    }
+    return $uri;
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php
index d48c6f2..b611cdc 100644
--- a/core/lib/Drupal/Core/Database/Database.php
+++ b/core/lib/Drupal/Core/Database/Database.php
@@ -7,6 +7,8 @@
 
 namespace Drupal\Core\Database;
 
+use GuzzleHttp\Psr7\Uri;
+
 /**
  * Primary front-controller for the database system.
  *
@@ -370,13 +372,8 @@
       throw new DriverNotSpecifiedException('Driver not specified for this database connection: ' . $key);
     }
 
-    if (!empty(self::$databaseInfo[$key][$target]['namespace'])) {
-      $driver_class = self::$databaseInfo[$key][$target]['namespace'] . '\\Connection';
-    }
-    else {
-      // Fallback for Drupal 7 settings.php.
-      $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
-    }
+    $namespace = !empty(self::$databaseInfo[$key][$target]['namespace']) ? self::$databaseInfo[$key][$target]['namespace'] : NULL;
+    $driver_class = static::getDatabaseConnectionClass($driver, $namespace);
 
     $pdo_connection = $driver_class::open(self::$databaseInfo[$key][$target]);
     $new_connection = new $driver_class($pdo_connection, self::$databaseInfo[$key][$target]);
@@ -460,36 +457,33 @@ public static function ignoreTarget($key, $target) {
    *   requirements.
    */
   public static function convertDbUrlToConnectionInfo($url, $root) {
-    $info = parse_url($url);
-    if (!isset($info['scheme'], $info['host'], $info['path'])) {
+    $uri = new Uri($url);
+    if (empty($uri->getHost()) || empty($uri->getHost()) || empty($uri->getPath())) {
       throw new \InvalidArgumentException('Minimum requirement: driver://host/database');
     }
-    $info += array(
-      'user' => '',
-      'pass' => '',
-      'fragment' => '',
-    );
-
-    // A SQLite database path with two leading slashes indicates a system path.
-    // Otherwise the path is relative to the Drupal root.
-    if ($info['path'][0] === '/') {
-      $info['path'] = substr($info['path'], 1);
-    }
-    if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') {
-      $info['path'] = $root . '/' . $info['path'];
-    }
 
-    $database = array(
-      'driver' => $info['scheme'],
-      'username' => $info['user'],
-      'password' => $info['pass'],
-      'host' => $info['host'],
-      'database' => $info['path'],
+    // Discover if the URL has a driver namespace.
+    $parts = array();
+    parse_str($uri->getQuery(), $parts);
+    $namespace = isset($parts['namespace']) ? $parts['namespace'] : '';
+
+    $fragment = $uri->getFragment();
+    $connection_opptions = array(
+      'driver' => $uri->getScheme(),
+      'host' => $uri->getHost(),
+      // Strip the first leading slash of the path to get the database name.
+      // Note that additional leading slashes have meaning for some database
+      // drivers.
+      'database' => substr($uri->getPath(), 1),
+      'prefix' => $fragment ?: NULL,
+      'namespace' => $namespace,
     );
-    if (isset($info['port'])) {
-      $database['port'] = $info['port'];
+
+    $driver_class = static::getDatabaseConnectionClass($connection_opptions['driver'], $connection_opptions['namespace']);
+    if (!class_exists($driver_class)) {
+      throw new \RuntimeException("Can not convert $url to a database connection, class $driver_class does not exist");
     }
-    return $database;
+    return $driver_class::convertDbUrlToConnectionInfoHelper($uri, $root, $connection_opptions);
   }
 
   /**
@@ -503,29 +497,49 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
    */
   public static function getConnectionInfoAsUrl($key = 'default') {
     $db_info = static::getConnectionInfo($key);
-    if ($db_info['default']['driver'] == 'sqlite') {
-      $db_url = 'sqlite://localhost/' . $db_info['default']['database'];
+    if (empty($db_info)) {
+      throw new \RuntimeException("Database connection $key not defined");
     }
-    else {
-      $user = '';
-      if ($db_info['default']['username']) {
-        $user = $db_info['default']['username'];
-        if ($db_info['default']['password']) {
-          $user .= ':' . $db_info['default']['password'];
-        }
-        $user .= '@';
-      }
 
-      $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host'];
-      if (isset($db_info['default']['port'])) {
-        $db_url .= ':' . $db_info['default']['port'];
-      }
-      $db_url .= '/' . $db_info['default']['database'];
+    $namespace = isset($db_info['default']['namespace']) ? $db_info['default']['namespace'] : NULL;
+    $driver_class = static::getDatabaseConnectionClass($db_info['default']['driver'], $namespace);
+    $uri = new Uri(NULL);
+    $uri = $uri
+      ->withScheme($db_info['default']['driver'])
+      ->withHost($db_info['default']['host'])
+      ->withPath($db_info['default']['database']);
+
+    if (!empty($db_info['default']['prefix']['default'])) {
+      $uri = $uri->withFragment($db_info['default']['prefix']['default']);
+    }
+    if (!empty($db_info['default']['namespace'])) {
+      $uri = Uri::withQueryValue($uri, 'namespace', $db_info['default']['namespace']);
     }
-    if ($db_info['default']['prefix']['default']) {
-      $db_url .= '#' . $db_info['default']['prefix']['default'];
+
+    $uri = $driver_class::getConnectionInfoAsUrlHelper($db_info['default'], $uri);
+    return (string) $uri;
+  }
+
+  /**
+   * Gets the fully qualified connection class name for a database driver.
+   *
+   * @param string $driver
+   *   The database driver to get the class for.
+   * @param string $namespace
+   *   A custom namespace to use.
+   *
+   * @return string
+   *   The fully qualified class name of the driver's database connection class.
+   */
+  public static function getDatabaseConnectionClass($driver, $namespace) {
+    if ($namespace) {
+      $driver_class = $namespace . '\\Connection';
+    }
+    else {
+      // Fallback for Drupal 7 settings.php.
+      $driver_class = 'Drupal\\Core\\Database\\Driver\\' . $driver . '\\Connection';
     }
-    return $db_url;
+    return $driver_class;
   }
 
 }
diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
index b94ae9f..daaf828 100644
--- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
+++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
@@ -10,6 +10,7 @@
 use Drupal\Core\Database\Database;
 use Drupal\Core\Database\DatabaseNotFoundException;
 use Drupal\Core\Database\Connection as DatabaseConnection;
+use GuzzleHttp\Psr7\Uri;
 
 /**
  * SQLite implementation of \Drupal\Core\Database\Connection.
@@ -415,4 +416,28 @@ public function getFullQualifiedTableName($table) {
     return $prefix . $table;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public static function convertDbUrlToConnectionInfoHelper(Uri $uri, $root, array $database) {
+    // A SQLite database with a leading slash indicates a system path.
+    // Otherwise the path is relative to the Drupal root.
+    if ($database['database'][0] !== '/') {
+      $database['database'] = $root . '/' . $database['database'];
+    }
+    return $database;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function getConnectionInfoAsUrlHelper(array $connection_options, Uri $uri) {
+    if ($connection_options['database'][0] === '/') {
+      // If the database is a absolute path add an additional leading slash to
+      // denote this.
+      $uri = $uri->withPath('/' . $connection_options['database']);
+    }
+    return $uri;
+  }
+
 }
