'psql' is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# 'psql' is not recognized as an internal or external command

The error "'psql' is not recognized as an internal or external command, operable program or batch file" occurs for multiple reasons:

  1. Not having PostgreSQL installed on your Windows machine.
  2. Not having the path to the psql executable in your system's PATH environment variable.

psql is not recognized as internal or external command

If you already have PostgreSQL installed, scroll down to the "Add the path to psql.exe to your system's PATH environment variable" subheading.

To install and configure PostgreSQL on Windows:

  1. Download the Open source PostgreSQL installer from EDB.

You can click on the download button under "Windows x86-64" for the latest version.

download postgresql installer

  1. When you start the installer, you might have to change your app recommendation settings.

change my app recommendation settings

  1. You can set the "Choose where to get apps" value to "Anywhere, but warn me before installing an app that's not from the Microsoft Store".

choose where to get apps

  1. Click on "Install anyway".

click install anyway

  1. Click the "Next" button on the Welcome screen.

postgres welcome screen click next

  1. Leave the default destination location selected and click "Next".

postgres select destination location

  1. Leave the default components checked and click on the "Next" button.

select postgres components

  1. Leave the default Data Directory selected and click "Next".

select data directory

  1. Set your password for the postgres user and make sure to make a note of it.

set password for postgres user

  1. Leave the default port number of 5432 selected and click "Next".

select server port number

  1. Leave the "Locale" setting set to [Default locale] to use the locale of the underlying operating system.

postgres select default locale

  1. Click "Next" on the "Pre-installation summary" page.

click-next-on-summary-page

  1. Click the "Next" button on the "Ready to Install" screen.

click next on ready to install screen

  1. Wait for the installation to complete.

  2. On the last screen, uncheck the "Stack builder may be used to install additional tools" checkbox and click "Finish".

uncheck checkbox and click finish

The next step is to add the path to the psql executable (psql.exe) to your system's PATH environment variable.

# Add the path to psql.exe to your system's PATH environment variable

To add the path to the psql executable to your system's PATH environment variable:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

click environment variables

  1. In the "System variables" section, select the "Path" variable and click "Edit".

select path and click edit

  1. You can either click "New" and then "Browse" and navigate to the path of the bin directory or paste it directly.

click new browse

For me, the path is the following.

cmd
# 👇️ this is my path C:\Program Files\PostgreSQL\15\bin # 👇️ might also be something like this C:\Program Files (x86)\PostgreSQL\15\bin

Note that the path contains your PostgreSQL version, so it will likely be different in your case.

path-to-psql-exe

We are looking for the directory that contains the psql.exe file because this is the file that is run when you issue psql commands.
  1. Once you find the path to your psql executable, add it and click on the "OK" button twice to confirm.

added path to psql exe

  1. Close your Command prompt application and then reopen it.
Note that you must restart your Command prompt shell for the changes to take effect.

You might also have to restart your PC, but that's not always necessary.

Open a new CMD shell and issue the psql --version command to make sure the psql executable is accessible.

cmd
psql --version

psql command works

You can use the psql -U postgres command to log in using the default postgres user.

Note that you will be prompted for the password you specified when installing PostgreSQL.
cmd
psql -U postgres

log in using default postgres user

Once you're logged in, you can use the CREATE DATABASE command to create a database.

cmd
CREATE DATABASE employee WITH encoding='UTF8';

postgres-create-database

Use the \c employee command to switch to the employee database.

cmd
\c employee

switch-to-database

You can use the CREATE TABLE command to create a table.

cmd
CREATE TABLE tasks(task_id serial PRIMARY KEY, task VARCHAR (255) UNIQUE NOT NULL);

postgres create table

Use the INSERT INTO command to insert a record into the table.

cmd
INSERT INTO tasks(task) VALUES('walk the dog');

psql insert into table

Use the SELECT FROM command to list the records in the tasks table.

cmd
SELECT * FROM tasks;

postgres select from table

You can also use the SQL Shell (psql) application to issue Postgres commands:

  1. Click on the Search bar, type "sql shell" and click on the "SQL Shell" (psql) application.

search sql shell

  1. Once you start the SQL Shell application you will be prompted for:
  • Server - press Enter to use the default of localhost
  • Database - press Enter to use the default of postgres
  • Port - press Enter to use the default of 5432
  • Username - press Enter to use the default of postgres
  • Password - type in the password you used when installing PostgreSQL

log in using sql shell

Once you enter your credentials, you will be logged in as the postgres user, which is the same as issuing the psql -U postgres command in CMD and entering your password.

cmd
psql -U postgres

log in using default postgres user

# Conclusion

To solve the error "'psql' is not recognized as an internal or external command, operable program or batch file", make sure:

  1. You have PostgreSQL installed on your Windows machine.
  2. You have the path to the psql executable in your system's PATH environment variable.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.