Skip to content

Commit 34c9784

Browse files
committed
Provide environment overrides for psql file locations.
PSQL_HISTORY provides an alternative for the command history file, and PSQLRC provides an alternative location for the .psqlrc file.
1 parent b59ca98 commit 34c9784

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3340,6 +3340,26 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
33403340
</listitem>
33413341
</varlistentry>
33423342

3343+
<varlistentry>
3344+
<term><envar>PSQL_HISTORY</envar></term>
3345+
3346+
<listitem>
3347+
<para>
3348+
Alternative location for the command history file. Tilde ("~") expansion is performed.
3349+
</para>
3350+
</listitem>
3351+
</varlistentry>
3352+
3353+
<varlistentry>
3354+
<term><envar>PSQLRC</envar></term>
3355+
3356+
<listitem>
3357+
<para>
3358+
Alternative location of the user's .psqlrc file. Tilde ("~") expansion is performed.
3359+
</para>
3360+
</listitem>
3361+
</varlistentry>
3362+
33433363
<varlistentry>
33443364
<term><envar>SHELL</envar></term>
33453365

@@ -3390,6 +3410,11 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
33903410
to set up the client or the server to taste (using the <command>\set
33913411
</command> and <command>SET</command> commands).
33923412
</para>
3413+
<para>
3414+
The location of the user's <filename>~/.psqlrc</filename> file can
3415+
also be set explicitly via the <envar>PSQLRC</envar> environment
3416+
setting.
3417+
</para>
33933418
</listitem>
33943419

33953420
<listitem>
@@ -3411,6 +3436,11 @@ PSQL_EDITOR_LINENUMBER_ARG='--line '
34113436
<filename>~/.psql_history</filename>, or
34123437
<filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
34133438
</para>
3439+
<para>
3440+
The location of the history file can
3441+
also be set explicitly via the <envar>PSQL_HISTORY</envar> environment
3442+
setting.
3443+
</para>
34143444
</listitem>
34153445
</itemizedlist>
34163446
</refsect1>

src/bin/psql/input.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,15 @@ initializeInput(int flags)
285285
history_lines_added = 0;
286286

287287
histfile = GetVariable(pset.vars, "HISTFILE");
288+
289+
if (histfile == NULL)
290+
{
291+
char * envhist;
292+
envhist = getenv("PSQL_HISTORY");
293+
if (envhist != NULL && strlen(envhist) > 0)
294+
histfile = envhist;
295+
}
296+
288297
if (histfile == NULL)
289298
{
290299
if (get_home_path(home))

src/bin/psql/startup.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,22 @@ process_psqlrc(char *argv0)
591591
char rc_file[MAXPGPATH];
592592
char my_exec_path[MAXPGPATH];
593593
char etc_path[MAXPGPATH];
594+
char *envrc;
594595

595596
find_my_exec(argv0, my_exec_path);
596597
get_etc_path(my_exec_path, etc_path);
597598

598599
snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
599600
process_psqlrc_file(rc_file);
600601

601-
if (get_home_path(home))
602+
envrc = getenv("PSQLRC");
603+
604+
if (envrc != NULL && strlen(envrc) > 0)
605+
{
606+
expand_tilde(&envrc);
607+
process_psqlrc_file(envrc);
608+
}
609+
else if (get_home_path(home))
602610
{
603611
snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
604612
process_psqlrc_file(rc_file);

0 commit comments

Comments
 (0)