diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 82b2cdf..4aa6020 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -523,11 +523,12 @@ SET ENABLE_SEQSCAN TO OFF;
       </indexterm>
       <listitem>
        <para>
-        On systems that support the <symbol>TCP_KEEPIDLE</symbol> socket option, specifies the
+        On systems that support the <symbol>TCP_KEEPIDLE</symbol> or
+        <symbol>TCP_KEEPALIVE</> socket option, specifies the
         number of seconds between sending keepalives on an otherwise idle
-        connection. A value of zero uses the system default. If <symbol>TCP_KEEPIDLE</symbol> is
-        not supported, this parameter must be zero. This parameter is ignored for
-        connections made via a Unix-domain socket.
+        connection. A value of zero uses the system default. If neither of
+        these socket options is supported, this parameter must be zero. This
+        parameter is ignored for connections made via a Unix-domain socket.
        </para>
       </listitem>
      </varlistentry>
diff --git a/doc/src/sgml/libpq.sgml b/doc/src/sgml/libpq.sgml
index 496d1a9..d6cb709 100644
--- a/doc/src/sgml/libpq.sgml
+++ b/doc/src/sgml/libpq.sgml
@@ -298,10 +298,10 @@
           <para>
            Controls the number of seconds of inactivity after which TCP should
            send a keepalive message to the server.  A value of zero uses the
-           system default.  This parameter is ignored if the
-           <symbol>TCP_KEEPIDLE</> socket option is not supported, for
-           connections made via a Unix-domain socket, or if keepalives are
-           disabled.
+           system default.  This parameter is ignored if the neither the
+           <symbol>TCP_KEEPIDLE</> nor the <symbol>TCP_KEEPALIVE</> socket
+           options are supported, for connections made via a Unix-domain
+           socket, or if keepalives are disabled.
           </para>
          </listitem>
         </varlistentry>
diff --git a/src/backend/libpq/pqcomm.c b/src/backend/libpq/pqcomm.c
index 82f5a59..6a60890 100644
--- a/src/backend/libpq/pqcomm.c
+++ b/src/backend/libpq/pqcomm.c
@@ -1349,7 +1349,7 @@ pq_setkeepalivesidle(int idle, Port *port)
 	if (port == NULL || IS_AF_UNIX(port->laddr.addr.ss_family))
 		return STATUS_OK;
 
-#ifdef TCP_KEEPIDLE
+#if defined(TCP_KEEPIDLE) || defined(TCP_KEEPALIVE)
 	if (idle == port->keepalives_idle)
 		return STATUS_OK;
 
@@ -1367,12 +1367,21 @@ pq_setkeepalivesidle(int idle, Port *port)
 	if (idle == 0)
 		idle = port->default_keepalives_idle;
 
+#ifdef TCP_KEEPIDLE
 	if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPIDLE,
 				   (char *) &idle, sizeof(idle)) < 0)
 	{
 		elog(LOG, "setsockopt(TCP_KEEPIDLE) failed: %m");
 		return STATUS_ERROR;
 	}
+#else
+	if (setsockopt(port->sock, IPPROTO_TCP, TCP_KEEPALIVE,
+				   (char *) &idle, sizeof(idle)) < 0)
+	{
+		elog(LOG, "setsockopt(TCP_KEEPALIVE) failed: %m");
+		return STATUS_ERROR;
+	}
+#endif
 
 	port->keepalives_idle = idle;
 #else
diff --git a/src/interfaces/libpq/fe-connect.c b/src/interfaces/libpq/fe-connect.c
index ed37bbd..d828391 100644
--- a/src/interfaces/libpq/fe-connect.c
+++ b/src/interfaces/libpq/fe-connect.c
@@ -1008,6 +1008,20 @@ setKeepalivesIdle(PGconn *conn)
 						  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
 		return 0;
 	}
+#else
+#ifdef TCP_KEEPALIVE
+	/* Darwin uses TCP_KEEPALIVE rather than TCP_KEEPIDLE */
+	if (setsockopt(conn->sock, IPPROTO_TCP, TCP_KEEPALIVE,
+				   (char *) &idle, sizeof(idle)) < 0)
+	{
+		char	sebuf[256];
+
+		appendPQExpBuffer(&conn->errorMessage,
+						  libpq_gettext("setsockopt(TCP_KEEPIDLE) failed: %s\n"),
+						  SOCK_STRERROR(SOCK_ERRNO, sebuf, sizeof(sebuf)));
+		return 0;
+	}
+#endif
 #endif
 
 	return 1;
