Hello all,

The attached patch modifies the message displayed by psql
when asking the password "Password: " 
to include the username as well:  "Password for user postgres : ". 

Displaying the username is useful when running sql scripts 
which are setting the session authorization (like the dump scripts
generated by older versions of pg_dump) and you don't have all the 
usernames/passwords in ~/.pgpass.    
In such cases it can happen that psql is asking several times :
Password: 
Password: 
Password: 
Password: 

and you don't know whether it's asking the pasword of the 'postgres' user
or the password of user that is importing the data . 

I have the feeling that asking for "Password:" several times no longer 
happens when running scripts generated by recent versions of pg_dump.
However, the patch might be useful for people who are upgrading .  


Cheers,
Adrian Maier
*** ./src/bin/psql/command.c.orig	2005-06-30 09:06:41.953634513 +0300
--- ./src/bin/psql/command.c	2005-06-30 09:49:41.216208791 +0300
***************
*** 912,917 ****
--- 912,918 ----
  	const char *dbparam = NULL;
  	const char *userparam = NULL;
  	const char *pwparam = NULL;
+ 	char       *password_prompt = NULL;
  	char	   *prompted_password = NULL;
  	bool		need_pass;
  	bool		success = false;
***************
*** 931,939 ****
  	else
  		userparam = new_user;
  
  	/* need to prompt for password? */
  	if (pset.getPassword)
! 		pwparam = prompted_password = simple_prompt("Password: ", 100, false);
  
  	/*
  	 * Use old password (if any) if no new one given and we are
--- 932,951 ----
  	else
  		userparam = new_user;
  
+ 	if (userparam == NULL) 
+ 	{
+ 		password_prompt = malloc(strlen("Password: ")+1);
+ 		sprintf(password_prompt,"Password: ");
+ 	}
+ 	else
+ 	{
+ 		password_prompt = malloc(strlen(userparam)+30);
+ 		sprintf(password_prompt,"Password for user %s : ",userparam);
+ 	}
+ 
  	/* need to prompt for password? */
  	if (pset.getPassword)
! 		pwparam = prompted_password = simple_prompt(password_prompt, 100, false);
  
  	/*
  	 * Use old password (if any) if no new one given and we are
***************
*** 957,967 ****
  			need_pass = true;
  			free(prompted_password);
  			prompted_password = NULL;
! 			pwparam = prompted_password = simple_prompt("Password: ", 100, false);
  		}
  	} while (need_pass);
  
  	free(prompted_password);
  
  	/*
  	 * If connection failed, try at least keep the old one. That's
--- 969,980 ----
  			need_pass = true;
  			free(prompted_password);
  			prompted_password = NULL;
! 			pwparam = prompted_password = simple_prompt(password_prompt, 100, false);
  		}
  	} while (need_pass);
  
  	free(prompted_password);
+ 	free(password_prompt);
  
  	/*
  	 * If connection failed, try at least keep the old one. That's
*** ./src/bin/psql/startup.c.orig	2005-06-30 09:06:34.816798114 +0300
--- ./src/bin/psql/startup.c	2005-06-30 09:22:50.487536877 +0300
***************
*** 106,111 ****
--- 106,112 ----
  
  	char	   *username = NULL;
  	char	   *password = NULL;
+ 	char       *password_prompt = NULL;
  	bool		need_pass;
  
  	set_pglocale_pgservice(argv[0], "psql");
***************
*** 187,194 ****
  			username = pg_strdup(options.username);
  	}
  
  	if (pset.getPassword)
! 		password = simple_prompt("Password: ", 100, false);
  
  	/* loop until we have a password if requested by backend */
  	do
--- 188,206 ----
  			username = pg_strdup(options.username);
  	}
  
+ 	if (username == NULL)
+ 	{
+ 		password_prompt = malloc(strlen("Password: ")+1);
+ 		strcat(password_prompt,"Password: "); 
+ 	}
+ 	else
+ 	{
+ 		password_prompt = malloc(strlen(username)+30);
+ 		sprintf(password_prompt,"Password for user %s : ",username); 
+ 	}
+ 	
  	if (pset.getPassword)
! 		password = simple_prompt(password_prompt, 100, false);
  
  	/* loop until we have a password if requested by backend */
  	do
***************
*** 206,217 ****
  			need_pass = true;
  			free(password);
  			password = NULL;
! 			password = simple_prompt("Password: ", 100, false);
  		}
  	} while (need_pass);
  
  	free(username);
  	free(password);
  
  	if (PQstatus(pset.db) == CONNECTION_BAD)
  	{
--- 218,230 ----
  			need_pass = true;
  			free(password);
  			password = NULL;
! 			password = simple_prompt(password_prompt, 100, false);
  		}
  	} while (need_pass);
  
  	free(username);
  	free(password);
+ 	free(password_prompt);
  
  	if (PQstatus(pset.db) == CONNECTION_BAD)
  	{
---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]

Reply via email to