diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index b4f9ad7..4bd30f1 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -6029,9 +6029,14 @@ PasswordFromFile(char *hostname, char *port, char *dbname, char *username)
 		if (len == 0)
 			continue;
 
-		/* Remove trailing newline */
+		/* Remove trailing newline, including msdos-style */
 		if (buf[len - 1] == '\n')
-			buf[len - 1] = 0;
+		{
+			if (len > 1 && buf[len - 2] == '\r')
+				buf[len - 2] = 0;
+			else
+				buf[len - 1] = 0;
+		}
 
 		if ((t = pwdfMatchesString(t, hostname)) == NULL ||
 			(t = pwdfMatchesString(t, port)) == NULL ||
