psql : The term 'psql' is not recognized as the name of a cmdlet, function, script file, or operable program.
— Windows cannot find psql.exe in any folder on your PATH. It is usually
installed, just not on the PATH: run it by its full path, or add its bin
folder to PATH and open a new terminal.
On the test PC, PostgreSQL 18 for Windows had put psql in
C:\Program Files\PostgreSQL\18\bin and neither the user nor the machine PATH
named that folder. If your PostgreSQL runs in Docker, you need no Windows psql at all:
the container has its own.
The error
psql : The term 'psql' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ psql -U postgres
+ ~~~~
+ CategoryInfo : ObjectNotFound: (psql:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
That is Windows PowerShell 5.1. cmd.exe said
'psql' is not recognized as an internal or external command, operable program or batch file.
and Git Bash said bash: psql: command not found. Same cause in all three.
Why it happens
A shell finds a bare command name by searching the folders listed in the PATH environment
variable. Installing a program does not by itself put its folder there. Here
where.exe psql answered INFO: Could not find files for the given
pattern(s)., while the file itself was easy to find:
PS> Get-ChildItem 'C:\Program Files\PostgreSQL\*\bin\psql.exe' | Select-Object FullName
FullName
--------
C:\Program Files\PostgreSQL\18\bin\psql.exe
The fix
PS> & 'C:\Program Files\PostgreSQL\18\bin\psql.exe' --version
psql (PostgreSQL) 18.3
PS> $env:Path += ';C:\Program Files\PostgreSQL\18\bin'
PS> psql --version
psql (PostgreSQL) 18.3
PS> (Get-Command psql).Source
C:\Program Files\PostgreSQL\18\bin\psql.exe
The & call operator runs a path with spaces in PowerShell. The
$env:Path line fixes the current window only. To keep it, open
Edit environment variables for your account from the Start menu, edit
Path, add C:\Program Files\PostgreSQL\18\bin (your version
number may differ) and open a new terminal; that permanent change was described here, not
made on the test PC. Git Bash takes the folder in its own path form:
$ export PATH="$PATH:/c/Program Files/PostgreSQL/18/bin"
$ psql --version
psql (PostgreSQL) 18.3
With PostgreSQL in a container, skip the Windows client and use the container's psql:
PS> docker exec fix6-pg18 psql -U postgres -c "SELECT current_user;"
current_user
--------------
postgres
(1 row)
For an interactive session add -it, as in
docker exec -it fix6-pg18 psql -U postgres, from a real terminal window; run
without one, it stopped with
cannot attach stdin to a TTY-enabled container because stdin is not a terminal.
How it was reproduced
Windows 11 with Windows PowerShell 5.1.26100 and Git Bash 5.2.26. PostgreSQL 18 for Windows
was already installed on the PC (psql 18.3) and its service on port 5432 was left alone.
The error was captured in a fresh powershell.exe -NoProfile process, in
cmd.exe and in Git Bash; the full-path connection went to a
postgres:18 test container (PostgreSQL 18.4) published on
127.0.0.1:15460, with the password set in PGPASSWORD for that process. The
container name fix6-pg18 is this test's own, and the container was removed afterwards.
Frequently asked
- Where is psql.exe on Windows?
- With the PostgreSQL installer for Windows it sits in the bin folder of the installation, C:\Program Files\PostgreSQL\18\bin\psql.exe for version 18 on the test PC. In PowerShell, Get-ChildItem 'C:\Program Files\PostgreSQL\*\bin\psql.exe' finds it for any installed version.
- How do I add psql to PATH on Windows?
- Open Edit environment variables for your account from the Start menu, edit Path, add the PostgreSQL bin folder, for example C:\Program Files\PostgreSQL\18\bin, confirm and open a new terminal. For the current PowerShell window only, append the folder to $env:Path.
- Can I use psql without installing PostgreSQL on Windows?
- Yes, when PostgreSQL runs in Docker, because the container has its own psql. Run docker exec -it with the container name and psql -U postgres from a terminal window for an interactive session, or docker exec without -it and psql -c for a single statement.
More decoded errors in the Fixes category. For the TTY message in detail, see docker exec -it: the input device is not a TTY.