Software⏱️ 4 min read📅 2026-06-11

How to Fix: Setting up PostgreSQL gives error sudo: initdb: command not found

Error fixing PostgreSQL initdb command not found in Ubuntu.

Quick Answer: Check if sudo is correctly configured and the PATH environment variable is set. Try running initdb with sudo -i to ensure it's executed as a login shell.

Setting up PostgreSQL with an error message 'sudo: initdb: command not found' can be frustrating for users, especially those new to Linux. This error occurs when the system cannot find the 'initdb' command, which is used to initialize a PostgreSQL database. The issue might arise from a missing or incorrectly configured environment variable in the system's PATH.

The error message suggests that the installation of PostgreSQL did not add the necessary directory to the system's PATH, leading to the 'command not found' error when trying to run 'initdb'. This can be fixed by adjusting the system's configuration and adding the necessary directories to the PATH variable.

🔍 Why This Happens

  • The primary reason for this error is that the PostgreSQL installation directory was not added to the system's PATH. When installing PostgreSQL using 'sudo apt-get install postgresql-9.1', the package manager only installs the necessary files in the '/usr/lib/postgresql/9.1/' directory, but does not add it to the system's PATH by default.
  • Another possible reason is that there are conflicting environment variables or configuration settings that prevent the 'initdb' command from being found.

✅ Best Solutions to Fix It

Adding the PostgreSQL directory to the system's PATH

  1. Step 1: Open the '/etc/profile' file using a text editor and add the following line at the bottom: export PATH=$PATH:/usr/lib/postgresql/9.1/bin. This will update the system's PATH variable to include the PostgreSQL installation directory.
  2. Step 2: Restart the terminal or run 'source /etc/profile' to apply the changes. The updated PATH variable should now include the '/usr/lib/postgresql/9.1/' directory.
  3. Step 3: Verify that the 'initdb' command can be found by running 'which initdb'. If it is not found, try running 'sudo which initdb' to see if the command is available with superuser privileges.

Using the 'sudo -i' command

  1. Step 1: Run the following command in a terminal: sudo -i. This will log you in as the superuser and update the environment variables, including the PATH variable.
  2. Step 2: Verify that the 'initdb' command can be found by running 'which initdb'. If it is not found, try running 'sudo which initdb' to see if the command is available with superuser privileges.

💡 Conclusion

To resolve the 'sudo: initdb: command not found' error, you need to add the PostgreSQL installation directory to the system's PATH variable. You can do this by adding the following line to the '/etc/profile' file and then restarting the terminal or running 'source /etc/profile'. Alternatively, you can use the 'sudo -i' command to log in as the superuser and update the environment variables.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions