How to start psql.exe

pgny picture pgny · Dec 2, 2017 · Viewed 37.7k times · Source

I'm new to postgresql (pg). I understand that in order to interact with pg, I must use psql.exe.

In my system, I find psql.exe not once, but twice, why? (C:\Program Files (x86)\pgAdmin 4\v2\runtime\psql.exe and C:\Program Files\PostgreSQL\10\bin\psql.exe). I tried both, with identical (negative) results.

In line with Q/A In PostgreSQL why does command line window disappear when I press Enter after entering my password?, I now, instead of running psql.exe directly, I first opened a generic (Windows) command window and then ran psql.exe from within this generic command window; I then entered the password and Enter.

In the generic (Windows) command window, I now got the message:

psql: could not connect to server: Permission denied (0x0000271D/10013)
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? FATAL: password authentication failed for user "User"

This corresponds to the problem described (but not resolved) in the Q/A cited above. Other threads in this forum re pg password fails concern Linux / outdated passwords / new passwords after password changes.

It's said in other threads here re this question that in Windows, there is a file pgpass.conf. I find a help file libpq-pgpass.html which also says that, while on my system, there is no such file pgpass.conf (or anything else with "pgpass" except for the aforementioned html file).

(W10 Prof 64 bit English)

Second strategy:

Within the StartMenu, there is an entry "PostgreSQL"; under that - available by Enter - there is a sub-entry "SQL Shell (psql)"; so I tried that.

From there only, I get the Windows cmd window, with "psql 10.1" and with (in [] it's obviously the default values):

Server [localhost]: I enter localhost; 
Database [postgres]: I enter postgres; 
Port [5432]: I enter 5432; 
Username [postgres]: I enter postgres; 
Password for user postgres: I enter the password which is "a".

From there, I get a warning re the Console code page which differs from Windows code page, but I then get the psql prompt

postgres=#

and which works.

On the other hand, with the first strategy above, I only get "Password:" and then, after entry of the real password (which is "a"), the "FATAL" error "password authentication failed for user "A"".

Upon installation, I had created a superuser "postgres" with password "a", not a user "A"; on the other hand, I am user "A" which is my Windows admin (!) account (no other users than myself on my pc) and the cmd prompt I usually get ("C:\Users\A>"); (but as expected,) no different result if I enter the whole path to psql.exe from the prompt "C:>".

So my problem comes now down to this: When triggered from within the start menu, psql.exe works correctly since it asks me all the questions I can then answer correctly (as stated above), while when started with its full path from the cmd window (both full paths tried and enclosed in "" of course), it just asks for the "password" and then tells me that for some "user A", that password is wrong.

Also, the full path to the (functioning) psql.exe from within the start menu is C:\ProgramData\Microsoft\Windows\Start Menu\Programs\PostgreSQL 10\psql.exe, and if I try to run that from with the cmd window, I get "(path) is not recognized as an internal or external command, operable program or batch file.", since in there, it's "SQL Shell (psql).lnk", of which the properties are: "Target: "C:\Program Files\PostgreSQL\10\scripts\runpsql.bat"".

So this is the solution, see my answer below.

Answer

pgny picture pgny · Dec 2, 2017

You don't run some psql.exe file, but the file C:\Program Files\PostgreSQL\10\scripts\runpsql.bat , for example programmatically, e.g. from AHK:

run, "C:\Program Files\PostgreSQL\10\scripts\runpsql.bat"

Then you get a generic Windows command window which asks you the right questions (see above) and, when answered correctly, gives you the psql command prompt.