44

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"
Community
  • 1
  • 1
umezo
  • 1,519
  • 1
  • 19
  • 33
  • Please let me know if there is any additional information I can add to clarify my question. Thanks in advance! – umezo Jul 13 '12 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"). –  Jul 19 '12 at 18:40

9 Answers9

62

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

Community
  • 1
  • 1
Micah Stubbs
  • 1,827
  • 21
  • 34
  • 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. http://www.kscodes.com/misc/how-to-set-path-in-windows-without-admin-rights/ – Magnus Jul 13 '21 at 11:34
22

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

Transformer
  • 3,642
  • 1
  • 22
  • 33
10

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\12\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.

Community
  • 1
  • 1
Jatin Chauhan
  • 105
  • 1
  • 7
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.

  • 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 Jul 19 '12 at 19:26
  • @umezo: the output is not the usual Windows output of an `echo $PATH`. What exactly are you using? –  Jul 19 '12 at 19:41
  • 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? – umezo Jul 19 '12 at 20:22
  • by "not get anything back," I mean that I return the literal string $PATH – umezo Jul 19 '12 at 20:24
  • @umezo: Apparently this must `echo %PATH%` in Windows. Sorry, my fault. –  Jul 19 '12 at 20:27
  • I get back a long list as before, which includes all of the directories that are in my path. This includes C:\PostgreSQL\9.1\bin; and C:\PostgreSQL\9.1\lib. Should I type up the whole list? (not sure if there's a way to copy text from the command prompt). – umezo Jul 19 '12 at 20:34
  • @umezo: if they are part of the path, then it must be something different. Where exactly are you running those commands. Is that really inside a Windows command prompt? –  Jul 19 '12 at 20:41
  • Yes, I am using the Windows command prompt. Can you think of any other cause for my inability to access pg through the command prompt? – umezo Jul 19 '12 at 20:51
  • You can also just type "PATH" and no need for the echo. – parchambeau Jan 24 '13 at 21:44
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

zhrist
  • 1,169
  • 11
  • 28
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.

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

lava
  • 6,020
  • 2
  • 31
  • 28
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

Amrutha VS
  • 144
  • 1
  • 1
  • 10
  • 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: http://www.kscodes.com/misc/how-to-set-path-in-windows-without-admin-rights/ – Magnus Jul 13 '21 at 11:27
-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 :)

Vignesh
  • 259
  • 4
  • 15
  • Adding it manually to your path should have worked fine. Maybe you didn't use the directory postgres actually installed to? – StringsOnFire Aug 16 '15 at 14:43