which
prints out the full path of the command it's passed. For example, try typing which pwd
into your command line. You should see /bin/pwd
as the result. This simply means that the executable command for pwd
is stored in the /bin
directory.
Like Mike noted, if you run which psql
, and the result is /usr/bin/psql
it means that the executable command for your Postgresql database server is in the /usr/bin
directory, which means Bash
isn't going to run the Postgres.app database server.
Bash is an old Unix acronym, which stands for 'Bourne-Again Shell' As you can probably guess, it's just a pun on the name of Stephen Bourne, who authored the original Bourne shell, which is the foundation for the command line interface in Unix. (Mac OS is based on Unix).
The .bash_profile
is a file that states which commands will be executed when you open your terminal. (aka Bash).
By changing the .bash_profile
as Mike explained, to include export PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
, you are changing the configuration of your Bash. This means that every time you open a terminal window, this new line will be taken into account.
PATH
is an environment variable. It is something that will exist for the life of a terminal session. (i.e. it gets created when you open a terminal window, and dies when you close it.) There are other environment variables, but PATH
is special in the way it's formatted.
In the example of PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
we are creating a variable that consists of directories. When a command is given/executed, Bash will search through each directory in PATH
until it finds the directory where the executable exists. Again, think back to psql
- this is a command that is executed when you type it in. Bash needs to know where it can execute that command, and we help out by specifying a PATH
variable in our .bash_profile
In this case Bash will find the code to execute psql
in the /Applications/Postgres.app/Contents/MacOS
directory.
Finally, the export
command is used to tell Bash to remove or add something from or to the PATH variable. In our example, we are adding the directory structure for our Postgres.app execution files to our PATH variable. This is because we want to export
it, or (in this case) add it to our Bash. The reason for this is because when we first executed which psql
, we saw that Bash wanted to execute psql
from the /usr/bin
directory. This is not what we wanted, so we essentially re-route Bash to pull the psql
from a new directory - stated in our modified (via export
) PATH
variable.
In plain English, our goal was to have our Bash (terminal window) be able to run a Postgres database server using Postgres.app. Originally, our Bash wasn't configured for this, so we told it where to find the correct executable, and configured our profile so that our Bash would default to using the desired Postgres database server.
This post provides greater detail into PATH
.