Have you ever installed PostgreSQL successfully, opened your terminal, and tried running:
psql --version
Only to receive an error like this?
'psql' is not recognized as an internal or external command,
operable program or batch file.
I recently encountered this problem while configuring PostgreSQL 19 on Windows.
Initially, I thought PostgreSQL had not installed correctly. However, after investigating, I discovered that PostgreSQL was already installed and working. The actual problem was that Windows could not locate the PostgreSQL executable through its PATH environment variable.
In this article, I'll explain what caused the problem and how I fixed it.
1. Understanding the Problem
After installing PostgreSQL 19, I opened Command Prompt and executed:
psql --version
Unfortunately, Windows returned:
'psql' is not recognized as an internal or external command,
operable program or batch file.
At first, this looked like an installation problem.
However, I decided to check whether PostgreSQL was actually installed before attempting another installation.
I executed:
dir "C:\Program Files\PostgreSQL"
Windows returned the following directories:
14
18
19
psqlODBC
This revealed that I had multiple PostgreSQL versions installed.
Important lesson: Before reinstalling a development tool, first establish whether it is already installed. Sometimes the problem is not the installation but the system configuration.
2. Understanding the PATH Environment Variable
Before fixing the problem, let's understand what PATH actually does.
PATH is an environment variable maintained by the operating system. It contains a list of directories where Windows looks for executable programs when you enter commands in your terminal.
For example, when you execute:
psql --version
Windows searches the directories registered in PATH to locate psql.exe.
If PostgreSQL's bin directory is missing from PATH, Windows cannot find the executable automatically.
PostgreSQL's executable was located here:
C:\Program Files\PostgreSQL\19\bin\psql.exe
The directory we needed to add to PATH was:
C:\Program Files\PostgreSQL\19\bin
Notice that we add the directory, not the executable file itself.
3. Confirming That PostgreSQL Was Installed Correctly
Instead of immediately changing system settings, I tested PostgreSQL using its complete executable path.
I executed:
"C:\Program Files\PostgreSQL\19\bin\psql.exe" --version
The output was:
psql (PostgreSQL) 19.0
This confirmed two things:
- PostgreSQL 19 was installed successfully.
- The
psql.execommand-line utility was working.
Therefore, the problem was not PostgreSQL itself. It was the PATH configuration.
This is an important troubleshooting technique: test the executable directly before modifying environment variables.
4. Fixing PostgreSQL in PowerShell and VS Code Terminal
If you're using PowerShell or the PowerShell terminal inside Visual Studio Code, you can temporarily add PostgreSQL to the current terminal session.
Execute:
$env:Path += ";C:\Program Files\PostgreSQL\19\bin"
Now test:
psql --version
Expected output:
psql (PostgreSQL) 19.0
That's it!
The command works because $env:Path accesses the PATH environment variable in PowerShell.
The += operator appends the PostgreSQL directory to the existing PATH.
Important: This is a temporary solution. The change applies only to the current PowerShell session. Closing the terminal removes that temporary change.
5. Making the Configuration Permanent
If you don't want to repeat the command every time you open your terminal, you need to add PostgreSQL to your Windows environment variables.
Follow these steps:
Step 1: Open Environment Variables
- Press
Windows + S. - Search for
Environment Variables. - Select
Edit the system environment variables. - Click
Environment Variables.
Step 2: Edit the PATH variable
- Under System variables, locate
Path. - Select it.
- Click
Edit. - Click
New. - Add the following directory:
C:\Program Files\PostgreSQL\19\bin
- Click OK to save your changes.
Step 3: Restart your terminal
Close your existing Command Prompt, PowerShell, and VS Code windows.
Open a new terminal and execute:
psql --version
You should now see:
psql (PostgreSQL) 19.0
The configuration is now available to newly opened terminals.
6. Testing Other PostgreSQL Commands
After fixing PATH, you can also test other PostgreSQL command-line utilities.
Check the PostgreSQL backup utility:
pg_dump --version
Check the database creation utility:
createdb --version
Check the database restoration utility:
pg_restore --version
These commands should work without specifying their complete installation paths.
7. Connecting to PostgreSQL
Now that the terminal recognizes PostgreSQL, you can connect to your database server.
Execute:
psql -U postgres -h localhost -p 5432
Explanation:
-
psql: Starts the PostgreSQL command-line client. -
-U postgres: Specifies the PostgreSQL username. -
-h localhost: Connects to the local database server. -
-p 5432: Specifies the PostgreSQL port.
Enter your PostgreSQL password when prompted.
If the connection succeeds, you'll see a prompt similar to:
postgres=#
You can now execute SQL queries directly from your terminal.
For example:
SELECT version();
8. Key Lessons for Developers
This experience taught me several important lessons about configuring development environments.
Installation does not always mean configuration is complete. A program can be installed correctly but remain inaccessible from the terminal.
Understand environment variables. PATH helps operating systems locate executable programs without requiring their full installation paths.
Test before reinstalling. Running an executable using its complete path helps determine whether the installation is actually broken.
Understand your terminal. CMD and PowerShell use different commands and syntax for working with environment variables.
Distinguish temporary from permanent changes. A PowerShell session-level PATH modification is not the same as updating Windows system PATH.
Conclusion
The PostgreSQL problem initially appeared to be an installation issue, but it turned out to be a simple PATH configuration problem.
By understanding how Windows locates executable programs, I was able to fix the issue without reinstalling PostgreSQL.
As developers, we should learn to investigate the actual cause of an error instead of immediately reinstalling software or changing configurations blindly.
Sometimes, the solution is not installing another tool. It's simply helping your operating system find the tool you already have.
Top comments (2)
The detail that would have saved me the most time is the one in section 1 — finding versions 14, 18, and 19 all sitting in C:\Program Files\PostgreSQL before assuming the install had failed. I have reinstalled tools over exactly this kind of confusion before. Testing the full path to psql.exe before touching PATH is the right order too; it turns a guess into a diagnosis.
One question: with three versions installed, adding 19\bin to PATH makes pg_dump and createdb resolve to 19 as well. Do you keep the older versions around for a reason, or do you uninstall them once 19 is confirmed working, so nothing accidentally runs against the wrong cluster?
I recommend keeping older PostgreSQL versions only if existing projects or databases still depend on them. Otherwise, once PostgreSQL 19 is confirmed to be working, uninstalling unused versions helps keep the development environment clean. Also, remember that changing PATH affects all PostgreSQL command-line tools, including psql, pg_dump, and createdb, so it's important to verify that they all point to the intended version using the where command.