DEV Community

Vincent Tommi
Vincent Tommi

Posted on

PostgreSQL: Why Is `psql` Not Working in Your Windows Terminal? (And How to Fix It Permanently)

Have you ever installed PostgreSQL successfully, opened your terminal, and tried running:

psql --version
Enter fullscreen mode Exit fullscreen mode

Only to receive an error like this?

'psql' is not recognized as an internal or external command,
operable program or batch file.
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Unfortunately, Windows returned:

'psql' is not recognized as an internal or external command,
operable program or batch file.
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Windows returned the following directories:

14
18
19
psqlODBC
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The directory we needed to add to PATH was:

C:\Program Files\PostgreSQL\19\bin
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The output was:

psql (PostgreSQL) 19.0
Enter fullscreen mode Exit fullscreen mode

This confirmed two things:

  • PostgreSQL 19 was installed successfully.
  • The psql.exe command-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"
Enter fullscreen mode Exit fullscreen mode

Now test:

psql --version
Enter fullscreen mode Exit fullscreen mode

Expected output:

psql (PostgreSQL) 19.0
Enter fullscreen mode Exit fullscreen mode

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

  1. Press Windows + S.
  2. Search for Environment Variables.
  3. Select Edit the system environment variables.
  4. Click Environment Variables.

Step 2: Edit the PATH variable

  1. Under System variables, locate Path.
  2. Select it.
  3. Click Edit.
  4. Click New.
  5. Add the following directory:
C:\Program Files\PostgreSQL\19\bin
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

You should now see:

psql (PostgreSQL) 19.0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check the database creation utility:

createdb --version
Enter fullscreen mode Exit fullscreen mode

Check the database restoration utility:

pg_restore --version
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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=#
Enter fullscreen mode Exit fullscreen mode

You can now execute SQL queries directly from your terminal.

For example:

SELECT version();
Enter fullscreen mode Exit fullscreen mode

8. Key Lessons for Developers

This experience taught me several important lessons about configuring development environments.

  1. Installation does not always mean configuration is complete. A program can be installed correctly but remain inaccessible from the terminal.

  2. Understand environment variables. PATH helps operating systems locate executable programs without requiring their full installation paths.

  3. Test before reinstalling. Running an executable using its complete path helps determine whether the installation is actually broken.

  4. Understand your terminal. CMD and PowerShell use different commands and syntax for working with environment variables.

  5. 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)

Collapse
 
userxnom profile image
Abdullah Naseer •

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?

Collapse
 
vincenttommi profile image
Vincent Tommi •

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.