How to set Environment Variable on PostgreSQL on Windows.

How to set Environment Variable on PostgreSQL on Windows.

·

3 min read

Did you receive this error too?
'psql' is not recognized as an internal or external command, operable program or batch file.

I was working on a codebase in which it was my first time installing Postgres. However, I had to spend a lot of time figuring out how to set Environment Variable.

This might be an issue mostly for Windows users so I hope this blog will be a reference for myself in the future and a bonus if it helps someone else gets unstuck.

Step 1: Download and Install Postgres

Feel free to jump to Step 2.

Go to the PostgreSQL official website (postgresql.org/download) and select Windows.

Download the .exe file and follow the instructions to install the software.

It will prompt you to setup a password too. I kept the Port on its default 5432 and didn't select any of the dropdown options on the Locale, I kept it as [Default locale].

Once it's completed, you can keep the checkbox for "Stack Builder" checked and click on the "Finish" button.

You will see a "Welcome to Stack Builder" with an empty dropdown.

There are 2 options, so select the 2nd option that says "PostgreSQL on port 5432"

After clicking on "Next" you will see "red arrows" that look like folders starting with "Categories", just click on the "Cancel" button and selected "Yes" when it asks if you are sure to close the Stack Builder wizard.

In the Search taskbar, you can search for pgAdmin to open the application.

Step 2: Set Local Environment Variable

Open Command Prompt or Git Bash and try running the command psql -U postgres.

if it asks for your password(the one you saved when installing) then that's great.

However, for myself, I received this error saying "'psql' is not recognized as an internal or external command, operable program or batch file."

The Solution:

Go into your "This PC" and find the folder where you installed postgres.

for me, the application was saved inside my OS drive and inside the Program Files(NOT program Files x86).

My pathway looked like this C:\Program Files\PostgreSQL\15

open the "bin" folder and copy the path

something like this: C:\Program Files\PostgreSQL\15\bin (my path will look different than yours)

Search in your taskbar "Edit the system environment variables" and select it.

It will open a "System Properties" window.

Make sure you have the "Advanced" tab open and click on the "Environment Variables"

Then select the "PATH"(it could be all in Uppercase or not) and click on "Edit"

Another window that says "Edit environment variable" will pop up.

Click on the "New" button and paste on the list the pathway we copied previously that ends with \bin

Go back to the Postgres folder and this time copy the pathway to the \lib folder and also add it to the Environment variable as you did with \bin.

Before following the next steps, I suggest you make sure your VS code is closed too.

You can search and open the Command Prompt and enter psql -U postgres

if it asks for your password then that's a good sign that it's working!

The Command Prompt might show the same error message, which did for me the first time. I would suggest you also try running this command psql -U postgres on Git Bash which worked for me.

Conclusion

By following the steps in this blog, you should be able to set the environment variable for PostgreSQL on Windows and resolve the 'psql' error message. I hope this helped someone.


Feel free to follow my blog and also follow me on Twitter and connect with me on LinkedIn. Have a wonderful day!