diff --git a/src/include/port.h b/src/include/port.h
index 0c2b2369215f4a2c75ffafb0fdbb5c9600d2a186..3d7f2675545c2d721a8d9f39fbf2e738a0b21119 100644
--- a/src/include/port.h
+++ b/src/include/port.h
@@ -301,6 +301,18 @@ extern FILE *pgwin32_fopen(const char *, const char *);
 #define		fopen(a,b) pgwin32_fopen(a,b)
 #endif
 
+/*
+ * Mingw-w64 headers #define popen and pclose to _popen and _pclose.  We want
+ * to use our popen wrapper, rather than plain _popen, so override that.  For
+ * consistency, use our version of pclose, too.
+ */
+#ifdef popen
+#undef popen
+#endif
+#ifdef pclose
+#undef pclose
+#endif
+
 /*
  * system() and popen() replacements to enclose the command in an extra
  * pair of quotes.