PostgreSQL command history on Windows

postgresqlpsqlwindows

I am trying to use psql from a Windows command prompt (cmd.exe), but can't access any of the previous commands with the up/down cursors. I could also really do with finding out where the PostgreSQL equivalent to .bash_history is (if there is one), as I could read the command history from there.

\s produces the following error:

history is not supported by this installation

Is there a way to get the psql command history on Windows?

Best Answer

On Windows, there isn't an exact equivalent to a .bash_history because cmd.exe is an intrinsically different type of shell. I think you're referring more to the .psql_history (see related question).

There is a "SQL Shell" available in the SQL Command Line Terminal of the EnterpriseDB Windows distribution of PostgreSQL that supports a command history. However, I prefer to use the Windows command prompt instead of the GUI.

The error message you are getting indicates that psql.exe was built without readline support (reference).

The best way I have found to run psql via the normal Windows command prompt is to create a shortcut:

Target: %SystemRoot%\system32\cmd.exe /U /c "chcp 1252 & C:\Progra~1\PostgreSQL\9.6\bin\psql.exe -U database_username -d database_name"

psql shortcut

The command history can be controlled via Command history Buffer size in the Options pane:

psql shortcut options

And you can adjust the scrollable output via Screen Buffer Height in the Layout pane:

psql shortcut layout

This will get you a history via the up and down arrows, but only within your current session. It will not be preserved across sessions.