How to Solve Error Message ‘psql: command not found’

Posted on

This article is about how to solve error message ‘psql:command not found’. The problem exist upon the execution of the command ‘psql’. This command is a command for running the PostgreSQL Command Console. So, in order for a user to be able to enter the PostgreSQL Command Console, just execute the ‘psql’ command in the command line interface. The following is an example of the execution of the ‘psql’ command in the command line interface :

[user@hostname ~]$ psql
-bash: psql: command not found
[user@hostname ~]$

As seen in the output of the above command execution, the command ‘psql’ is unknown. The operating system does not understand how to interpret or to translate it into a certain running process. So, the following is the actual process or step to solve the problem :

1. The first step is checking the existing environment variable of the operating system. In this article, for an example it is the environment variable with the name of PATH. Just type the following command to print the value of it :

[centos@dc-socketsinergi ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin
[user@hostname ~]$ 

As it exist in the above output command execution, there is no indication or whatsoever about the PostgreSQL psql tool path. Since the path of the psql tool is actually in this context exist in ‘/opt/postgresql’ for an example. Just make sure that the ‘psql’ tool program exist in that path. Usually, it exist in the binary folder of the PostgreSQL Database Server’s installation. The following is the content of the folder :

[user@hostname postgresql]$ tree bin -L 1
bin
├── clusterdb
├── createdb
├── createuser
├── dropdb
├── dropuser
├── ecpg
├── initdb
├── pg_archivecleanup
├── pg_basebackup
├── pgbench
├── pg_checksums
├── pg_config
├── pg_controldata
├── pg_ctl
├── pg_dump
├── pg_dumpall
├── pg_isready
├── pg_receivewal
├── pg_recvlogical
├── pg_resetwal
├── pg_restore
├── pg_rewind
├── pg_test_fsync
├── pg_test_timing
├── pg_upgrade
├── pg_waldump
├── postgres
├── postmaster -> postgres
├── psql
├── reindexdb
└── vacuumdb

0 directories, 31 files
[user@hostname postgresql]$

As in the above output command execution, there is a file with the name of ‘psql’ inside the postgresql folder. So, make sure to include the path above to the environment variable of the operating system.

2. Because the path where the location of ‘psql’ which is in this context ‘/opt/postgresql/bin’ is not exist in the environment variable, just add it. Adding the ‘psql’ path is very easy. Just edit the .bashrc file where it exist in the home folder of the current logged-in user. Just edit the ‘.bashrc’ file and add the following line in the bottom part of the file :

export PSQL_PATH=/opt/postgresql/bin/
export PATH="$PATH:$PSQL_PATH"

3. Save the ‘.bashrc’ file and then don’t forget to activate the .bashrc file so the new added environment variable will be active. Just Execute the following command :

[user@hostname ~]$ source ~/.bashrc
[user@hostname ~]$

4. Don’t forget to display again the environment variable by executing the following command :

[user@hostname ~]$ echo $PATH
/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/postgresql/bin/:/home/centos/.local/bin:/home/centos/bin:/opt/postgresql/bin/
[centos@dc-socketsinergi ~]$

5. Since the environment variable is already include the ‘psql’ tool, just try to execute it again by typing the following command :

[user@hostname ~]$ echo $PSQL_PATH
/opt/postgresql/bin/
[user@hostname ~]$ which psql
/opt/postgresql/bin/psql
[user@hostname postgresql]$ 

6. Try to execute the ‘psql’ tool program again in the command line interface as follows :

[centos@dc-socketsinergi data]$ psql -Umyuser
psql (xxxxx)
Type "help" for help.

postgres=#

As it shows in the above output command, the execution of the ‘psql’ tool program is a success. The execution of the command will bring and redirect the process to the PostgreSQL Command Line Interface.

Leave a Reply