diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index be5e34a369a24f7b69ff1629dfdc5cedf74e3f93..3dea92c7d8f3bb6868754746f7cc1290713ca885 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -1645,11 +1645,11 @@ session_username(void)
  * substitute '~' with HOME or '~username' with username's home dir
  *
  */
-char *
+void
 expand_tilde(char **filename)
 {
 	if (!filename || !(*filename))
-		return NULL;
+		return;
 
 	/*
 	 * WIN32 doesn't use tilde expansion for file names. Also, it uses tilde
@@ -1697,5 +1697,5 @@ expand_tilde(char **filename)
 	}
 #endif
 
-	return *filename;
+	return;
 }
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index d8bb0930b415bb58caa158533568a50c5ba07456..db645dafabb4ee4c22d61cf946a7cd4eca8c31d0 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -44,6 +44,6 @@ extern bool is_superuser(void);
 extern bool standard_strings(void);
 extern const char *session_username(void);
 
-extern char *expand_tilde(char **filename);
+extern void expand_tilde(char **filename);
 
 #endif   /* COMMON_H */
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 5cb6b5f364866f08e8178f12cb578b1805b525f0..5d7fe6ea27f46c38fe7de72db39058ee3ac09640 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -610,7 +610,7 @@ process_psqlrc(char *argv0)
 	char		rc_file[MAXPGPATH];
 	char		my_exec_path[MAXPGPATH];
 	char		etc_path[MAXPGPATH];
-	char	   *envrc;
+	char	   *envrc = getenv("PSQLRC");
 
 	find_my_exec(argv0, my_exec_path);
 	get_etc_path(my_exec_path, etc_path);
@@ -618,12 +618,13 @@ process_psqlrc(char *argv0)
 	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
 	process_psqlrc_file(rc_file);
 
-	envrc = getenv("PSQLRC");
-
 	if (envrc != NULL && strlen(envrc) > 0)
 	{
-		expand_tilde(&envrc);
-		process_psqlrc_file(envrc);
+		/* might need to free() this */
+		char *envrc_alloc = pstrdup(envrc);
+
+		expand_tilde(&envrc_alloc);
+		process_psqlrc_file(envrc_alloc);
 	}
 	else if (get_home_path(home))
 	{