Setting up PostgreSQL gives error sudo: initdb: command not found
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.
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.
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
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