diff --git a/pgadmin/frm/frmConfig.cpp b/pgadmin/frm/frmConfig.cpp
index bc768ed..7f32ea3 100644
--- a/pgadmin/frm/frmConfig.cpp
+++ b/pgadmin/frm/frmConfig.cpp
@@ -326,7 +326,7 @@ void frmConfig::OpenLastFile()
 	if (file.IsOpened())
 	{
 #ifdef __WXMSW__
-		_setmode(file.fd(), _O_BINARY);
+//		_setmode(file.fd(), _O_BINARY);
 #endif
 		wxString buffer;
 		file.Read(buffer);
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 6dc1f1d..af87986 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..e0f3f86 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,58 @@ 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
+		{
+			if(!wxFileName::FileExists(fname))
+			{
+				/*wxFileName::DirName() does not return the directory path of the file
+				but it returns absolute path including file name thats why RemoveLastDir() is used in
+				the next line to remove the fileName
+				*/
+				wxFileName dirTemp = wxFileName::DirName(fname);
+				dirTemp.RemoveLastDir();
+				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();
+					}
+					if (!dir.Mkdir(0755, 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_IRUSR | wxS_IWUSR );
+
 		return fname;
 	}
+
 	return wxT("");
 }
 
