diff --git a/configure.in b/configure.in
index 0a529fa..bb1d0dd 100644
--- a/configure.in
+++ b/configure.in
@@ -390,6 +390,11 @@ choke me
 
 AC_SUBST(SUN_STUDIO_CC)
 
+# Check if it's Clang, which defines __clang__.
+AC_TRY_COMPILE([], [@%:@ifndef __clang__
+choke me
+@%:@endif], [CLANG=yes], [CLANG=no])
+
 unset CFLAGS
 
 #
@@ -1102,7 +1107,7 @@ AC_TYPE_INTPTR_T
 AC_TYPE_UINTPTR_T
 AC_TYPE_LONG_LONG_INT
 
-AC_CHECK_TYPES([struct cmsgcred, struct fcred, struct sockcred], [], [],
+AC_CHECK_TYPES([struct ucred,struct cmsgcred, struct fcred, struct sockcred], [], [],
 [#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/socket.h>
diff --git a/src/backend/libpq/auth.c b/src/backend/libpq/auth.c
index b06b391..27482bd 100644
--- a/src/backend/libpq/auth.c
+++ b/src/backend/libpq/auth.c
@@ -1786,7 +1786,7 @@ ident_unix(int sock, char *ident_user)
 	strlcpy(ident_user, pass->pw_name, IDENT_USERNAME_MAX + 1);
 
 	return true;
-#elif defined(SO_PEERCRED)
+#elif defined(SO_PEERCRED) && defined(HAVE_STRUCT_UCRED)
 	/* Linux style: use getsockopt(SO_PEERCRED) */
 	struct ucred peercred;
 	ACCEPT_TYPE_ARG3 so_len = sizeof(peercred);
diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in
index fd169b6..8d41549 100644
--- a/src/include/pg_config.h.in
+++ b/src/include/pg_config.h.in
@@ -529,6 +529,9 @@
 /* Define to 1 if `tm_zone' is member of `struct tm'. */
 #undef HAVE_STRUCT_TM_TM_ZONE
 
+/* Define to 1 if the system has the type `struct ucred'. */
+#undef HAVE_STRUCT_UCRED
+
 /* Define to 1 if you have the <SupportDefs.h> header file. */
 #undef HAVE_SUPPORTDEFS_H
 
diff --git a/src/template/linux b/src/template/linux
index 68da3bb..aff39e8 100644
--- a/src/template/linux
+++ b/src/template/linux
@@ -1,7 +1,9 @@
 # $PostgreSQL$
 
 # Force _GNU_SOURCE on; plperl is broken with Perl 5.8.0 otherwise
-CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
+if test "$CLANG" != yes; then
+  CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE"
+fi
 
 # If --enable-profiling is specified, we need -DLINUX_PROFILE
 PLATFORM_PROFILE_FLAGS="-DLINUX_PROFILE"
