Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start psql.exe

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?, 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 hit 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 regarding 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 Start menu, 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.

like image 794
pgny Avatar asked Dec 02 '17 10:12

pgny


People also ask

How do I start PostgreSQL in terminal?

Getting a PostgreSQL command prompt You can get a command shell in Windows by running cmd.exe. The CSEP544 shell launcher script will also open a shell for you. Type psql -U postgres at the prompt, and hit Enter. Here, postgres represents the username of the database superuser.


2 Answers

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.

like image 172
pgny Avatar answered Oct 24 '22 01:10

pgny


open command prompt first (Winkey+R), then type C:\Program Files\PostgreSQL\10\bin\psql.exe, then press enter and type in password. This is different from what you did by not closing psql.exe after you get error.

After you see the error, please read it carefully and lookfor solution in SO or google (or even manual documentation). If you still fail - post another question.

3rd party edit

To use psql.exe under windows you may need to start it with the postgres user

REM at the console of cmd.exe type
C:\PostgreSQL\bin>psql.exe -U postgres
REM this should result in this output
Password for user postgres:
like image 29
Vao Tsun Avatar answered Oct 24 '22 01:10

Vao Tsun