49

I cannot access PostgreSQL through the command line in Windows. Although I am able to create and update the databases, access them through PGAdminIII, and push to Heroku, I am unable to access them directly through my command line using the psql command.

When I try to create a database by using the command

$ createdb mydb

as explained in the PG documentation, I get the message

createdb: command not found

The documentation suggests that in this case, "PostgreSQL was not installed properly. Try calling the command with an absolute path instead." Sure enough, I am able to create a database by entering the direct path to the file:

$ c:/postgreSQL/9.1/bin/createdb mydb

Does this mean that the path can be reconfigured to connect correctly? Or do I need to reinstall? I've reinstalled several times, and have not been able to set the correct path. I currently have both the /bin and /lib paths addedto my system and user(:Umezo) paths, but still no access from the command line.

User variables for umezo
variable name: Path
variable value: C:\PostgreSQL\9.1\bin; C:\PostgreSQL\9.1\lib

System variables
variable name: Path
variable value: C:\PostgreSQL\9.1\bin; C:\PostgreSQL\9.1\lib

Any information or feedback regarding how I can fix my installment would be appreciated.

My info is as below:

I mostly followed these sources here and here. I installed a 32 bit version of 9.1 because some sources suggested issues with 64 bit.

PG download version: Installer Version 9.1 Win x86-32
Operating System: Windows 7 (64 bit)

pg_hba.conf

#TYPE     DATABASE     USER     CIER-ADDRESS     METHOD
# IPv4 local connections:
host      all          all      127.0.0.1/32     md5
# IPv6 local connections:
host      all          all      ::1/128          md5    

postgresql.conf

# - Connection Settings -
listen_addresses = '*'
port = 5432
max_connections = 100

pg_ident.conf

# *Everything is commented out*

Login Role Umezo #from properties window in PGAdminIII

Properties/Role name: Umezo
Properties/Can login: "check"
Role privileges: all categories "check"
2
  • Please let me know if there is any additional information I can add to clarify my question. Thanks in advance! Commented Jul 13, 2012 at 18:49
  • The .conf files are totally irrelevant to this question. This is a Windows only problem ("How to add a directory to the PATH"). Commented Jul 19, 2012 at 18:40

10 Answers 10

68

configuring postreSQL PATH variable on Windows 7

I encountered this issue too. I'm using Git Bash, hence the Unix-style $ prompt on Windows.

$ rails db
Couldn't find database client: psql, psql.exe.  Check your $PATH and try again.

Here's what I did:

In Windows 7, navigate to:

Control Panel
  All Control Panel Items
    System
      Advanced System Settings
        Environment Variables
          from the System Variables box select "PATH"
              Edit...

Then append this string to the existing PATH Variable Value:

;C:\Program Files\PostgreSQL\9.2\bin

and click "OK" three times to exit the menus.

Now, close the console and restart it.

Navigate back to the directory of your Rails app. In my case, this is accomplished with:

$ cd rails_projects/sample_app

Then, try again:

$ rails db

sources:

How do I put PostgreSQL /bin directory on my path in Windows?

http://railscasts.com/episodes/342-migrating-to-postgresql?view=asciicast

Sign up to request clarification or add additional context in comments.

1 Comment

I might add, there is another way to change the PATH, which does not need admin rights. This comes in handy if you are in restricted/managed work environment. kscodes.com/misc/…
24

Settings Windows Path For Postgresql

open my Computer ==>
  right click inside my computer and select properties ==>
    Click on Advanced System Settings ==>
       Environment Variables ==>
          from the System Variables box select "PATH" ==>
             Edit... ==>

then add this at the end of whatever you find their

 ;C:\PostgreSQL\9.2\bin; C:\PostgreSQL\9.2\lib

after that continue to click OK

open cmd/command prompt.... open psql in command prompt with this

psql -U username database

eg. i have a database name FRIENDS and a user MEE.. it will be

psql -U MEE FRIENDS

you will be then prompted to give the password of the user in question. Thanks

Comments

13

Set path For PostgreSQL in Windows:

  1. Searching for env will show Edit environment variables for your account
  2. Select Environment Variables
  3. From the System Variables box select PATH
  4. Click New (to add new path)

Change the PATH variable to include the bin directory of your PostgreSQL installation.
then add new path their....[for example]

C:\Program Files\PostgreSQL\16\bin

After that click OK

Open CMD/Command Prompt. Type this to open psql

psql -U username database_name

For Example psql -U postgres test

Now, you will be prompted to give Password for the User. (It will be hidden as a security measure).

Then you are good to go.

2 Comments

Between 3 and 4 you actually want to click "Edit" after selecting "Path" and then you will click "New" in the edit window for "Path". Thanks though.
Even on Postgres 16. The code snippet works. C:\Program Files\PostgreSQL\16\bin. Thanks Mate :)
9

All you need to do is to change the PATH variable to include the bin directory of your PostgreSQL installation.

An explanation on how to change environment variables is here:

http://support.microsoft.com/kb/310519
http://www.computerhope.com/issues/ch000549.htm

To verify that the path is set correctly, you can use:

echo %PATH%

on the commandline.

9 Comments

Thanks @a_horse_with_no_name. I actually do have the bin directory in my system and user paths (I have updated the question to reflect this). Using echo $PATH returns two instances each (once each for system and user, respectively) of c:/postgresql/9.1/bin and c:/postgresql/9.1/lib, which are the paths to the installment. Do you have an idea of why this may not be connecting?
@umezo: the output is not the usual Windows output of an echo $PATH. What exactly are you using?
I got the list from my command line in Aptana Studio 3. However, when I tried the same in a standard Windows command prompt, I now realize I don't get anything back. Could this be related to the issue, or am I entering the command ("echo $PATH") incorrectly?
by "not get anything back," I mean that I return the literal string $PATH
@umezo: Apparently this must echo %PATH% in Windows. Sorry, my fault.
|
3

On Postgres 9.6(PgAdmin 4) , this can be set up in Preferences->Paths->Binary paths: - set PostgreSQL Binary Path variable to "C:\Program Files\PostgreSQL\9.6\bin" or where you have installed

Comments

2

In order to connect my git bash to the postgreSQL, I had to add at least 4 environment variables to the windows. Git, Node.js, System 32 and postgreSQL. This is what I set as the value for the Path variable: C:\Windows\System32;C:\Program Files\Git\cmd;C:\Program Files\nodejs;C:\Program Files\PostgreSQL\12\bin; and It works perfectly.

Comments

2

Set Environment in Windows

Our createdbfile exists in this marked folder .so copy this folder path and paste inside environment variables setting under system variables.

enter image description here

start -> Environment Variables

enter image description here

select Environment variable

enter image description here

Create a new environment variable like this

enter image description here

Add environment variable postgres as %postgres% in the path variable

enter image description here

Now check by taking a new command prompt and typing conda (close already opened terminal or cmd)

enter image description here

Now live

Comments

1

Incase any one still wondering how to add environment variables then please use this link to add variables. Link: https://sqlbackupandftp.com/blog/setting-windows-path-for-postgres-tools

1 Comment

This works BUT i ran into a follow up problem. As i am in an administrator managed work environment, changing the PATH variable requires admin rights. Our IT did as your link suggests. However this only changed the PATH for the sysadmin-account, not my account Postgres was originally set up for (this might be a very specific problem). Interestingly you can change the PATH without admin rights as well, see here: kscodes.com/misc/…
0

If you want to run psql on a Window Terminal you can do the following:

  1. Open the windows terminal
  2. Type: @set PATH=%PATH%;C:\Program Files\PostgreSQL\16\bin;C:\Program Files\PostgreSQL\16\lib Change to the PostgreSQL version that you have. For me it is 16 (you can check the version in the path above)
  3. Run echo %path% and see if these environmental variables have been added
  4. You can now type psql --version to check if psql is now running

Unfortunately, you have to repeat step 2 at each terminal session because for some reasons these environment vars do not stick even when adding them manually in the Windows settings.

Comments

-1

I am using Windows 8 and the above solutions did not work out for me. I downgraded Postgres from 9.4 to 9.3. Man,it worked :)

1 Comment

Adding it manually to your path should have worked fine. Maybe you didn't use the directory postgres actually installed to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.