diff --git a/pgadmin/frm/frmPgpassConfig.cpp b/pgadmin/frm/frmPgpassConfig.cpp
index 19ed7db..5aeec97 100644
--- a/pgadmin/frm/frmPgpassConfig.cpp
+++ b/pgadmin/frm/frmPgpassConfig.cpp
@@ -61,10 +61,10 @@ frmPgpassConfig::frmPgpassConfig(frmMain *parent)
 
 	lastPath = sysSettings::GetConfigFile(sysSettings::PGPASS);
 	wxFile f;
-	if (!f.Exists(lastPath))
-		f.Create(lastPath, false, wxS_DEFAULT);
-
-	OpenLastFile();
+	if (f.Exists(lastPath))
+	{
+	    OpenLastFile();
+	}
 
 	helpMenu->Enable(MNU_HINT, false);
 	toolBar->EnableTool(MNU_HINT, false);
diff --git a/pgadmin/schema/pgServer.cpp b/pgadmin/schema/pgServer.cpp
index caba8b1..3e302e7 100644
--- a/pgadmin/schema/pgServer.cpp
+++ b/pgadmin/schema/pgServer.cpp
@@ -610,12 +610,11 @@ void pgServer::StorePassword()
 {
 	wxString fname = passwordFilename();
 
-	wxUtfFile file;
 	if (!wxFile::Exists(fname))
 	{
-		file.Create(fname, false, S_IREAD | S_IWRITE);
+		return;
 	}
-
+        wxUtfFile file;
 	// Don't try to read and write in one OP - it doesn't work well
 	wxString before;
 	file.Open(fname, wxFile::read, wxFONTENCODING_SYSTEM);
diff --git a/pgadmin/utils/sysSettings.cpp b/pgadmin/utils/sysSettings.cpp
index e9f270c..4e0e90c 100644
--- a/pgadmin/utils/sysSettings.cpp
+++ b/pgadmin/utils/sysSettings.cpp
@@ -23,12 +23,12 @@
 #include <wx/url.h>
 #include <wx/stdpaths.h>
 #include <wx/wfstream.h>
-
+#include <wx/filename.h>
+#include <wx/dir.h>
 // App headers
 #include "utils/sysSettings.h"
 #include "utils/sysLogger.h"
 #include "utils/misc.h"
-
 sysSettings::sysSettings(const wxString &name) : wxConfig(name)
 {
 	// Open the default settings file
@@ -762,6 +762,7 @@ void sysSettings::SetCanonicalLanguage(const wxLanguage &lang)
 //////////////////////////////////////////////////////////////////////////
 wxString sysSettings::GetConfigFile(configFileName cfgname)
 {
+        wxASSERT_MSG(cfgname == sysSettings::PGPASS,wxT("Handles only pgpass configuration"));
 	if (cfgname == PGPASS)
 	{
 #if wxCHECK_VERSION(2, 9, 5)
@@ -769,27 +770,56 @@ wxString sysSettings::GetConfigFile(configFileName cfgname)
 #else
 		wxStandardPaths stdp;
 #endif
-		wxString fname = stdp.GetUserConfigDir();
-#ifdef WIN32
-		fname += wxT("\\postgresql");
-		if (!wxDirExists(fname))
-			wxMkdir(fname);
-		switch(cfgname)
+		wxString fname;
+		bool bpsfile = wxGetEnv(wxT("PGPASSFILE"),&fname);
+		if (!bpsfile)
 		{
-			case PGPASS:
-				fname += wxT("\\pgpass.conf");
-				break;
+			fname = stdp.GetUserConfigDir();
+#ifdef WIN32
+			fname += wxT("\\postgresql");
+			if (!wxDirExists(fname))
+				wxMkdir(fname);
+			fname += wxT("\\pgpass.conf");
 		}
 #else
-		switch(cfgname)
-		{
-			case PGPASS:
-				fname += wxT("/.pgpass");
-				break;
+			fname += wxT("/.pgpass");
 		}
 #endif
+		else
+		{
+			wxFileName pgPassFile(fname);
+			if(!pgPassFile.FileExists(fname))
+			{
+				wxFileName dirTemp = wxFileName::DirName(fname);
+				if (!dirTemp.DirExists())
+				{
+					wxFileName dir = dirTemp;
+					//decide which are folders we need to create
+					wxString sDirPresent = dirTemp.GetPath();
+					while(!dirTemp.DirExists())
+					{
+						sDirPresent = dirTemp.GetPath();
+						dirTemp.RemoveLastDir();
+					}
+
+					dir.RemoveLastDir();
+					if (!dir.Mkdir(0777,wxPATH_MKDIR_FULL))
+					{
+						//In case of failure decide if we have created any folder if yes, delete it
+						if (wxDir::Exists(sDirPresent))
+							wxFileName::Rmdir(sDirPresent);
+					}
+				}
+			}
+		}
+
+                wxFile f;
+		if (!f.Exists(fname))
+			f.Create(fname, false, wxS_DEFAULT);
+
 		return fname;
 	}
+
 	return wxT("");
 }
 
