diff --git a/lib/setup.php b/lib/setup.php
index 71a2d13937e70e1af668a30f1f2ec8cbd7d9026f..1cbc83eed9a30d7ac7bc9a9bfbd4ae876b422c69 100644
--- a/lib/setup.php
+++ b/lib/setup.php
@@ -320,7 +320,13 @@ class OC_Setup {
 		$connection_string = "host='$e_host' dbname=postgres user='$e_user' password='$e_password'";
 		$connection = @pg_connect($connection_string);
 		if(!$connection) {
-			throw new DatabaseSetupException($l->t('PostgreSQL username and/or password not valid'));
+			// Try if we can connect to the DB with the specified name
+			$e_dbname = addslashes($dbname);
+			$connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
+			$connection = @pg_connect($connection_string);
+
+			if(!$connection)
+				throw new DatabaseSetupException($l->t('PostgreSQL username and/or password not valid'));
 		}
 		$e_user = pg_escape_string($dbuser);
 		//check for roles creation rights in postgresql