diff --git a/contrib/pg_upgrade/info.c b/contrib/pg_upgrade/info.c
index 756e8ffd8598238dd86197b9f99b7aaa65c4b4d6..19a7b8bda8c00ffc02257ff18c832320534259aa 100644
--- a/contrib/pg_upgrade/info.c
+++ b/contrib/pg_upgrade/info.c
@@ -228,7 +228,7 @@ get_db_infos(migratorContext *ctx, DbInfoArr *dbinfs_arr, Cluster whichCluster)
 				  "FROM pg_catalog.pg_database d "
 				  " LEFT OUTER JOIN pg_catalog.pg_tablespace t "
 				  " ON d.dattablespace = t.oid "
-				  "WHERE d.datname != 'template0'");
+				  "WHERE d.datallowconn = true");
 				  
 	i_datname = PQfnumber(res, "datname");
 	i_oid = PQfnumber(res, "oid");