alternate psql file locations

Started by Andrew Dunstanabout 14 years ago7 messages
#1Andrew Dunstan
andrew@dunslane.net

It's not a big thing, but I just found myself in a shared environment
wanting to be able to set alternative locations for the psql startup
file and history. I know there's the HISTFILE variable, but I can't
easily set that automatically unless I can at least have my own .psqlrc.
ISTM it should be a fairly simple thing to provide these, via
environment variables. Is there general interest in such a thing?

cheers

andrew

#2Alvaro Herrera
alvherre@commandprompt.com
In reply to: Andrew Dunstan (#1)
Re: alternate psql file locations

Excerpts from Andrew Dunstan's message of sáb dic 31 12:52:02 -0300 2011:

It's not a big thing, but I just found myself in a shared environment
wanting to be able to set alternative locations for the psql startup
file and history. I know there's the HISTFILE variable, but I can't
easily set that automatically unless I can at least have my own .psqlrc.
ISTM it should be a fairly simple thing to provide these, via
environment variables. Is there general interest in such a thing?

I wanted such a thing mere two weeks ago ...

--
Álvaro Herrera <alvherre@commandprompt.com>
The PostgreSQL Company - Command Prompt, Inc.
PostgreSQL Replication, Consulting, Custom Development, 24x7 support

#3Aidan Van Dyk
aidan@highrise.ca
In reply to: Alvaro Herrera (#2)
Re: alternate psql file locations

On Sat, Dec 31, 2011 at 3:17 PM, Alvaro Herrera
<alvherre@commandprompt.com> wrote:

Excerpts from Andrew Dunstan's message of sáb dic 31 12:52:02 -0300 2011:

It's not a big thing, but I just found myself in a shared environment
wanting to be able to set alternative locations for the psql startup
file and history. I know there's the HISTFILE variable, but I can't
easily set that automatically unless I can at least have my own .psqlrc.
ISTM it should be a fairly simple thing to provide these, via
environment variables. Is there general interest in such a thing?

I wanted such a thing mere two weeks ago ...

Generally when I've wanted these things, I just make a new "$HOME" in
my shared user home dir:

export HOME=$HOME/aidan

It's worked for things I've wanted, I haven't tried it for psql stuff....

a.

--
Aidan Van Dyk                                             Create like a god,
aidan@highrise.ca                                       command like a king,
http://www.highrise.ca/                                   work like a slave.

#4Andrew Dunstan
andrew@dunslane.net
In reply to: Aidan Van Dyk (#3)
1 attachment(s)
Re: alternate psql file locations

On 12/31/2011 04:26 PM, Aidan Van Dyk wrote:

On Sat, Dec 31, 2011 at 3:17 PM, Alvaro Herrera
<alvherre@commandprompt.com> wrote:

Excerpts from Andrew Dunstan's message of sáb dic 31 12:52:02 -0300 2011:

It's not a big thing, but I just found myself in a shared environment
wanting to be able to set alternative locations for the psql startup
file and history. I know there's the HISTFILE variable, but I can't
easily set that automatically unless I can at least have my own .psqlrc.
ISTM it should be a fairly simple thing to provide these, via
environment variables. Is there general interest in such a thing?

I wanted such a thing mere two weeks ago ...

Generally when I've wanted these things, I just make a new "$HOME" in
my shared user home dir:

export HOME=$HOME/aidan

It's worked for things I've wanted, I haven't tried it for psql stuff....

Yeah, but it's a bit hacky. I might well not want $HOME reset. Here's a
small patch that does what I think would suit me and Alvaro.

cheers

andrew

Attachments:

psqlenv.patchtext/x-patch; name=psqlenv.patchDownload
*** a/doc/src/sgml/ref/psql-ref.sgml
--- b/doc/src/sgml/ref/psql-ref.sgml
***************
*** 3300,3305 **** PSQL_EDITOR_LINENUMBER_ARG='--line '
--- 3300,3325 ----
     </varlistentry>
  
     <varlistentry>
+     <term><envar>PSQL_HISTORY</envar></term>
+ 
+     <listitem>
+      <para>
+       Alternative location for the command history file. Tilde ("~") expansion is performed.
+      </para>
+     </listitem>
+    </varlistentry>
+ 
+    <varlistentry>
+     <term><envar>PSQLRC</envar></term>
+ 
+     <listitem>
+      <para>
+       Alternative location of the user's .psqlrc file. Tilde ("~") expansion is performed.
+      </para>
+     </listitem>
+    </varlistentry>
+ 
+    <varlistentry>
      <term><envar>SHELL</envar></term>
  
      <listitem>
***************
*** 3349,3354 **** PSQL_EDITOR_LINENUMBER_ARG='--line '
--- 3369,3379 ----
       to set up the client or the server to taste (using the <command>\set
       </command> and <command>SET</command> commands).
      </para>
+     <para>
+      The location of the user's <filename>~/.psqlrc</filename> file can
+      also be set explicitly via the <envar>PSQLRC</envar> environment
+      setting.
+     </para>
     </listitem>
  
     <listitem>
***************
*** 3370,3375 **** PSQL_EDITOR_LINENUMBER_ARG='--line '
--- 3395,3405 ----
       <filename>~/.psql_history</filename>, or
       <filename>%APPDATA%\postgresql\psql_history</filename> on Windows.
      </para>
+     <para>
+      The location of the history file can
+      also be set explicitly via the <envar>PSQL_HISTORY</envar> environment
+      setting.
+     </para>
     </listitem>
    </itemizedlist>
   </refsect1>
*** a/src/bin/psql/input.c
--- b/src/bin/psql/input.c
***************
*** 285,290 **** initializeInput(int flags)
--- 285,299 ----
  		history_lines_added = 0;
  
  		histfile = GetVariable(pset.vars, "HISTFILE");
+ 
+ 		if (histfile == NULL)
+ 		{
+ 			char * envhist;
+ 			envhist = getenv("PSQL_HISTORY");
+ 			if (envhist != NULL && strlen(envhist) > 0)
+ 				histfile = envhist;
+ 		}
+ 
  		if (histfile == NULL)
  		{
  			if (get_home_path(home))
*** a/src/bin/psql/startup.c
--- b/src/bin/psql/startup.c
***************
*** 575,580 **** process_psqlrc(char *argv0)
--- 575,581 ----
  	char		rc_file[MAXPGPATH];
  	char		my_exec_path[MAXPGPATH];
  	char		etc_path[MAXPGPATH];
+ 	char       *envrc;
  
  	find_my_exec(argv0, my_exec_path);
  	get_etc_path(my_exec_path, etc_path);
***************
*** 582,588 **** process_psqlrc(char *argv0)
  	snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
  	process_psqlrc_file(rc_file);
  
! 	if (get_home_path(home))
  	{
  		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
  		process_psqlrc_file(rc_file);
--- 583,596 ----
  	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);
! 	}
! 	else if (get_home_path(home))
  	{
  		snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
  		process_psqlrc_file(rc_file);
#5Andrew Dunstan
andrew@dunslane.net
In reply to: Andrew Dunstan (#4)
Re: alternate psql file locations

On 01/01/2012 04:05 PM, Andrew Dunstan wrote:

On 12/31/2011 04:26 PM, Aidan Van Dyk wrote:

On Sat, Dec 31, 2011 at 3:17 PM, Alvaro Herrera
<alvherre@commandprompt.com> wrote:

Excerpts from Andrew Dunstan's message of sáb dic 31 12:52:02 -0300
2011:

It's not a big thing, but I just found myself in a shared environment
wanting to be able to set alternative locations for the psql startup
file and history. I know there's the HISTFILE variable, but I can't
easily set that automatically unless I can at least have my own
.psqlrc.
ISTM it should be a fairly simple thing to provide these, via
environment variables. Is there general interest in such a thing?

I wanted such a thing mere two weeks ago ...

Generally when I've wanted these things, I just make a new "$HOME" in
my shared user home dir:

export HOME=$HOME/aidan

It's worked for things I've wanted, I haven't tried it for psql
stuff....

Yeah, but it's a bit hacky. I might well not want $HOME reset. Here's
a small patch that does what I think would suit me and Alvaro.

Incidentally, this actually doesn't work anyway. psql gets the home path
from getpwuid() and ignores $HOME. You could argue that that's a bug,
but it's been that way for a long time.

cheers

andrew

#6Bruce Momjian
bruce@momjian.us
In reply to: Andrew Dunstan (#5)
Re: alternate psql file locations

On Mon, Jan 2, 2012 at 11:18:47AM -0500, Andrew Dunstan wrote:

It's worked for things I've wanted, I haven't tried it for psql
stuff....

Yeah, but it's a bit hacky. I might well not want $HOME reset.
Here's a small patch that does what I think would suit me and
Alvaro.

Incidentally, this actually doesn't work anyway. psql gets the home
path from getpwuid() and ignores $HOME. You could argue that that's
a bug, but it's been that way for a long time.

So, do we want to fix this and honor $HOME?

--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com

+ It's impossible for everything to be true. +

#7Andrew Dunstan
andrew@dunslane.net
In reply to: Bruce Momjian (#6)
Re: alternate psql file locations

On 08/26/2012 10:13 PM, Bruce Momjian wrote:

On Mon, Jan 2, 2012 at 11:18:47AM -0500, Andrew Dunstan wrote:

It's worked for things I've wanted, I haven't tried it for psql
stuff....

Yeah, but it's a bit hacky. I might well not want $HOME reset.
Here's a small patch that does what I think would suit me and
Alvaro.

Incidentally, this actually doesn't work anyway. psql gets the home
path from getpwuid() and ignores $HOME. You could argue that that's
a bug, but it's been that way for a long time.

So, do we want to fix this and honor $HOME?

Not really. Mangling it is a nasty hack anyway. Meanwhile see the
subsequent 9.2 feature (described thus in the release notes):

*

Provide environment variable overrides for psql history and startup
file locations (Andrew Dunstan)

Specifically, PSQL_HISTORY and PSQLRC determine these file names if set.

cheers

andrew