diff --git a/src/backend/libpq/be-secure-common.c b/src/backend/libpq/be-secure-common.c
index a3edf27e86c6800780794183e961254e3317eaee..bee618abb931f17262a271c9a1393403132e4789 100644
--- a/src/backend/libpq/be-secure-common.c
+++ b/src/backend/libpq/be-secure-common.c
@@ -112,9 +112,10 @@ run_ssl_passphrase_command(const char *prompt, bool is_server_start, char *buf,
 		goto error;
 	}
 
-	/* strip trailing newline */
+	/* strip trailing newline, including \r in case we're on Windows */
 	len = strlen(buf);
-	if (len > 0 && buf[len - 1] == '\n')
+	while (len > 0 && (buf[len - 1] == '\n' ||
+					   buf[len - 1] == '\r'))
 		buf[--len] = '\0';
 
 error:
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index a8048ffad209dd810a19003fdf66749b120afec6..b93ed7662b616e34d573e5220944a2a61c2e93f3 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -4661,6 +4661,8 @@ parseServiceFile(const char *serviceFile,
 
 	while ((line = fgets(buf, sizeof(buf), f)) != NULL)
 	{
+		int			len;
+
 		linenr++;
 
 		if (strlen(line) >= sizeof(buf) - 1)
@@ -4673,16 +4675,18 @@ parseServiceFile(const char *serviceFile,
 			return 2;
 		}
 
-		/* ignore EOL at end of line */
-		if (strlen(line) && line[strlen(line) - 1] == '\n')
-			line[strlen(line) - 1] = 0;
+		/* ignore EOL at end of line, including \r in case it's a DOS file */
+		len = strlen(line);
+		while (len > 0 && (line[len - 1] == '\n' ||
+						   line[len - 1] == '\r'))
+			line[--len] = '\0';
 
 		/* ignore leading blanks */
 		while (*line && isspace((unsigned char) line[0]))
 			line++;
 
 		/* ignore comments and empty lines */
-		if (strlen(line) == 0 || line[0] == '#')
+		if (line[0] == '\0' || line[0] == '#')
 			continue;
 
 		/* Check for right groupname */