diff -durpN postgresql.1/doc/src/sgml/ref/pg_basebackup.sgml postgresql.2/doc/src/sgml/ref/pg_basebackup.sgml
--- postgresql.1/doc/src/sgml/ref/pg_basebackup.sgml	2012-08-24 09:49:22.960530329 +0200
+++ postgresql.2/doc/src/sgml/ref/pg_basebackup.sgml	2012-10-10 14:49:40.869336237 +0200
@@ -189,6 +189,36 @@ PostgreSQL documentation
      </varlistentry>
 
      <varlistentry>
+      <term><option>-R</option></term>
+      <term><option>--write-recovery-conf</option></term>
+      <listitem>
+
+       <para>
+        Write a minimal recovery.conf into the output directory using
+        the connection parameters from the command line to ease
+        setting up the standby. Since creating a backup for a standalone
+        server with <option>-x</option> or <option>-X</option> and adding
+        a recovery.conf to it wouldn't make a working standby, these options
+        naturally conflict.
+       </para>
+
+       <para>
+        The password written into recovery.conf is not escaped even if special
+        characters appear in it. The administrator must review recovery.conf
+        to ensure proper escaping.
+       </para>
+
+       <para>
+        When this option is specified and taking the base backup succeeded,
+        failing to write the <filename>recovery.conf</filename> results
+        in the error code 2. This way, scripts can distinguish between different
+        failure cases of <application>pg_basebackup</application>.
+       </para>
+
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
       <term><option>-x</option></term>
       <term><option>--xlog</option></term>
       <listitem>
diff -durpN postgresql.1/src/bin/pg_basebackup/pg_basebackup.c postgresql.2/src/bin/pg_basebackup/pg_basebackup.c
--- postgresql.1/src/bin/pg_basebackup/pg_basebackup.c	2012-10-03 10:40:48.297207389 +0200
+++ postgresql.2/src/bin/pg_basebackup/pg_basebackup.c	2012-10-10 14:53:18.717764755 +0200
@@ -46,6 +46,7 @@ int			compresslevel = 0;
 bool		includewal = false;
 bool		streamwal = false;
 bool		fastcheckpoint = false;
+bool		writerecoveryconf = false;
 int			standby_message_timeout = 10 * 1000;		/* 10 sec = default */
 
 /* Progress counters */
@@ -70,14 +71,18 @@ static int	has_xlogendptr = 0;
 static volatile LONG has_xlogendptr = 0;
 #endif
 
+static PQconninfoOption *connOptions = NULL;
+
 /* Function headers */
 static void usage(void);
 static void verify_dir_is_empty_or_create(char *dirname);
 static void progress_report(int tablespacenum, const char *filename);
+static void stderr_write_error(FILE *cf, char *filename);
 
 static void ReceiveTarFile(PGconn *conn, PGresult *res, int rownum);
 static void ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum);
 static void BaseBackup(void);
+static void WriteRecoveryConf(void);
 
 static bool reached_end_position(XLogRecPtr segendpos, uint32 timeline,
 					 bool segment_finished);
@@ -107,6 +112,8 @@ usage(void)
 	printf(_("\nOptions controlling the output:\n"));
 	printf(_("  -D, --pgdata=DIRECTORY receive base backup into directory\n"));
 	printf(_("  -F, --format=p|t       output format (plain (default), tar)\n"));
+	printf(_("  -R, --write-recovery-conf\n"
+			 "                         write recovery.conf after backup\n"));
 	printf(_("  -x, --xlog             include required WAL files in backup (fetch mode)\n"));
 	printf(_("  -X, --xlog-method=fetch|stream\n"
 			 "                         include required WAL files with specified method\n"));
@@ -960,6 +967,10 @@ BaseBackup(void)
 		/* Error message already written in GetConnection() */
 		exit(1);
 
+	/* If recovery.conf is to be written, keep the connection parameters for later usage */
+	if (writerecoveryconf)
+		connOptions = PQconninfo(conn, true);
+
 	/*
 	 * Run IDENTIFY_SYSTEM so we can get the timeline
 	 */
@@ -1234,6 +1245,73 @@ BaseBackup(void)
 }
 
 
+static void
+stderr_write_error(FILE *cf, char *filename)
+{
+	fprintf(stderr, _("%s: cannot write to %s: %s"), progname, filename, strerror(errno));
+	fclose(cf);
+	unlink(filename);
+	exit(2);
+}
+
+
+/*
+ * Attempt to create recovery.conf and write the expected contents to it.
+ */
+static void
+WriteRecoveryConf(void)
+{
+	char		filename[MAXPGPATH];
+	PQconninfoOption   *option;
+	FILE		   *cf;
+
+	if (!writerecoveryconf)
+		return;
+
+	sprintf(filename, "%s/recovery.conf", basedir);
+
+	cf = fopen(filename, "w");
+	if (cf == NULL)
+	{
+		fprintf(stderr, _("%s: cannot create %s: %s"), progname, filename, strerror(errno));
+		exit(2);
+	}
+
+	if (fprintf(cf, "standby_mode = 'on'\n") < 0)
+		stderr_write_error(cf, filename);
+
+	if (fprintf(cf, "primary_conninfo = '") < 0)
+		stderr_write_error(cf, filename);
+
+	for (option = connOptions; option && option->keyword; option++)
+	{
+		/*
+		 * Do not emit this setting if not set, empty or default.
+		 * The list of options was already pre-filtered for options
+		 * usable for replication with PQconninfo(conn, true).
+		 */
+		if (option->val == NULL ||
+				(option->val != NULL && option->val[0] == '\0') ||
+				(option->val &&
+					option->compiled &&
+					strcmp(option->val, option->compiled) == 0))
+			continue;
+
+		/* write "keyword='value'" pieces, single quotes doubled */
+		if (fprintf(cf, "%s=''%s'' ", option->keyword, option->val) < 0)
+			stderr_write_error(cf, filename);
+	}
+
+	if (fprintf(cf, "'\n") < 0)
+		stderr_write_error(cf, filename);
+
+	PQconninfoFree(connOptions);
+
+	fclose(cf);
+
+	fprintf(stderr, _("%s: recovery.conf written. Please, add escaping to the password field in \"primary_conninfo\" if needed.\n"), progname);
+}
+
 int
 main(int argc, char **argv)
 {
@@ -1243,6 +1321,7 @@ main(int argc, char **argv)
 		{"pgdata", required_argument, NULL, 'D'},
 		{"format", required_argument, NULL, 'F'},
 		{"checkpoint", required_argument, NULL, 'c'},
+		{"write-recovery-conf", no_argument, NULL, 'R'},
 		{"xlog", no_argument, NULL, 'x'},
 		{"xlog-method", required_argument, NULL, 'X'},
 		{"gzip", no_argument, NULL, 'z'},
@@ -1280,7 +1359,7 @@ main(int argc, char **argv)
 		}
 	}
 
-	while ((c = getopt_long(argc, argv, "D:F:xX:l:zZ:c:h:p:U:s:wWvP",
+	while ((c = getopt_long(argc, argv, "D:F:RxX:l:zZ:c:h:p:U:s:wWvP",
 							long_options, &option_index)) != -1)
 	{
 		switch (c)
@@ -1301,6 +1380,9 @@ main(int argc, char **argv)
 					exit(1);
 				}
 				break;
+			case 'R':
+				writerecoveryconf = true;
+				break;
 			case 'x':
 				if (includewal)
 				{
@@ -1466,6 +1548,13 @@ main(int argc, char **argv)
 	}
 #endif
 
+	if (writerecoveryconf && includewal)
+	{
+		fprintf(stderr,
+				_("--xlog and --writerecoveryconf are mutually exclusive\n"));
+		exit(1);
+	}
+
 	/*
 	 * Verify that the target directory exists, or create it. For plaintext
 	 * backups, always require the directory. For tar backups, require it
@@ -1476,5 +1565,7 @@ main(int argc, char **argv)
 
 	BaseBackup();
 
+	WriteRecoveryConf();
+
 	return 0;
 }
