diff --git a/src/interfaces/libpq/fe-secure-openssl.c b/src/interfaces/libpq/fe-secure-openssl.c
index 00b203cbfa..1f536bbb13 100644
--- a/src/interfaces/libpq/fe-secure-openssl.c
+++ b/src/interfaces/libpq/fe-secure-openssl.c
@@ -1489,6 +1489,7 @@ open_client_SSL(PGconn *conn)
 {
 	int			r;
 
+	SOCK_ERRNO_SET(0);
 	ERR_clear_error();
 	r = SSL_connect(conn->ssl);
 	if (r <= 0)
@@ -1508,8 +1509,26 @@ open_client_SSL(PGconn *conn)
 			case SSL_ERROR_SYSCALL:
 				{
 					char		sebuf[PG_STRERROR_R_BUFLEN];
-
-					if (r == -1)
+					int			save_errno;
+					unsigned long vcode;
+
+					/*
+					 * Make sure OpenSSL doesn't clobber on the socket errno
+					 */
+					save_errno = SOCK_ERRNO;
+					vcode = SSL_get_verify_result(conn->ssl);
+					SOCK_ERRNO_SET(save_errno);
+
+					/*
+					 * If we get an X509 error here without an error in the
+					 * socket layer it means that verification failed without
+					 * it being a protocol error. A common cause is trying to
+					 * a default system CA which is missing or broken.
+					 */
+					if (!SOCK_ERRNO && vcode != X509_V_OK)
+						libpq_append_conn_error(conn, "SSL error: certificate verify failed: %s",
+												X509_verify_cert_error_string(ecode));
+					else if (r == -1)
 						libpq_append_conn_error(conn, "SSL SYSCALL error: %s",
 										  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 					else
