Step 1: I installed PostgreSQL using sudo apt-get install postgresql-9.1 as recommended on the PostgreSQL website

Step 2: I tried to run postgres. It's not found. For whatever reason, the install doesn't appear to add it to the path? So I had to manually add the line export PATH=$PATH:/usr/lib/postgresql/9.1/bin to the bottom of my ~/.profile. (Sidenote: Anybody know why this is necessary? Am I doing something wrong with the install? Everything else I've installed in Ubuntu "just works" without changing the $PATH...)

Step 3: I try running initdb /usr/local/var/postgres. Permission denied. I try running sudo initdb /usr/local/var/postgres. Result is sudo: initdb: command not found. How is this command not found? I just ran the damn thing! echo sudo $PATH shows the PostgreSQL directory in the path... what am I missing?

I'm a bit of a newbie in Linux, but these are the sorts of super-irritating problems I keep running into!

UPDATE: I believe it's related to this question. However, running the command with sudo -i does not fix the problem. I just get: -bash: initdb: command not found. Great.

UPDATE: This seems even closer. So I added alias sudo='sudo env PATH=$PATH' to my .bashrc as instructed. Still doesn't effin' work! It looks like the alias isn't working. When I run alias, I only show a single one. And yet my .bashrc is full of them... so something is wrong with those getting set up.

UPDATE: Since I'm using Ubuntu and RVM, RVM recommended that I set up the terminal to "Run command as login shell". Based on reading I did here, it seems that the .bashrc file isn't read in a login shell, only profile. So I moved the alias line from .bashrc to .profile, so .profile now has this at the end:

export PATH="/usr/lib/postgresql/9.1/bin:$PATH"
alias sudo='sudo env PATH=$PATH'

... and it still doesn't work. Running alias only shows an RVM alias, but not the sudo alias I tried to set up.

UPDATE From this site, I read about the precedence of dotfiles. It looks like .bash_profile comes before .profile. That being said, my PATH additions were done in .profile, and seemed to be loaded just fine, so why wasn't the alias also working? Moving the alias into .bash_profile from .profile worked, however. Mystery. So then the alias command shows my new alias. I finally type in sudo initdb /usr/local/var/postgres, to be met with: initdb: cannot be run as root. Oh, really? Then why were you giving me permission errors?! So now I think the problem is that I just have to chown the folder, but still run initdb as my user rather than root.

UPDATE Running the command sudo chown myuser /usr/local/var/postgres/, and then running initdb afterward allowed the database to be initialized. Glad it was so obvious that the directory permissions needed to be set to myuser and not root. Incredible. Successful database init 4 hours later.

5 Answers 5


Once you've installed the PostgreSQL server with apt-get install postgresql-9.1, it's already set up and running so your steps #2 and #3 (run postgres and run initdb) are not necessary and in fact would conflict with what has already been set up. By running manually initdb you'll end up with two postgres clusters.

The postgresql packages for Debian and Ubuntu provide a command named pg_createcluster that resides in /usr/bin and that you may call, should you need to create a new cluster. Under the hood, this command will call initdb with the right parameters. The end user is not supposed to call initdb directly, which is why it's not in the normal PATH. There are also other commands like pg_dropcluster and pg_ctlcluster that supersede the ways a non-packaged PostgreSQL instance would be administrated.

All this is meant for the packaging system to handle and upgrade automatically the clusters, which are features that the baseline PostgreSQL leaves to the database administrator.

  • I came to this thread because I want to initiate postgresql data directories in a custom location after updating relevant directories in the /etc/posgresql/14/main/postgresql.conf. There are use cases, such as mine, where one needs to run initdb.
    – Neil
    Commented Oct 1, 2022 at 22:18

When you run sudo it is best to put the full path to the command you are running. In your instance:

sudo /usr/lib/postgresql/9.1/bin/initdb /usr/local/var/postgres
  • 2
    I'm sure that will work, but why am I the only person having this problem? None of the tutorials or sites I'm looking at tell you to preface the command with the entire path.
    – aardvarkk
    Commented Dec 1, 2012 at 20:39
  • When you run sudo it reads the setting set in the sudoers file. There are several setting related to environment and if it should be reset or maintained. This post speaks about maintaining the environment, superuser.com/questions/232231/… if that is what you want to do.
    – Lipongo
    Commented Dec 1, 2012 at 20:42

I had this problem with an Amazon Web Services instance. The Postgresql site assumes you have the server and client installed.

sudo yum install postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs


It's most likely that initdb was not installed into a $PATH location. Check your paths (where the system looks for executables)


echo $PATH



To find where initdb was installed, you can just look for it directly


find / -name initdb


/usr/lib/postgresql/9.1/bin/initdb /usr/local/bin/

You have two choices in this case. Either add the postgres directory to $PATH or do a mv into one of the locations listed in $PATH

 mv /usr/lib/postgresql/9.1/bin/initdb /usr/local/bin/

As a result, initdb should give you some kind of output.

 initdb: no data directory specified
 You must identify the directory where the data for this database system
 will reside.  Do this with either the invocation option -D or the
 environment variable PGDATA.

Make sure to move the Postgres binary into the same location as initdb


You can simply make a symbolic link to a path known by the environment, e.g:

sudo ln -s /usr/lib/postgresql/9.1/bin/initdb /usr/local/bin/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .