Setting up PostgreSQL gives error sudo: initdb: command not found
Solution 1
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.
Solution 2
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
Solution 3
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
Solution 4
It's most likely that initdb was not installed into a $PATH
location. Check your paths (where the system looks for executables)
Command:
echo $PATH
Output:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
To find where initdb
was installed, you can just look for it directly
Command:
find / -name initdb
Output:
/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
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
Solution 5
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/
Related videos on Youtube
Comments
-
aardvarkk over 1 year
Step 1: I installed PostgreSQL using
sudo apt-get install postgresql-9.1
as recommended on the PostgreSQL websiteStep 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 lineexport 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 runningsudo initdb /usr/local/var/postgres
. Result issudo: 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 runalias
, 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 thealias
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 thesudo
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, myPATH
additions were done in.profile
, and seemed to be loaded just fine, so why wasn't thealias
also working? Moving thealias
into.bash_profile
from.profile
worked, however. Mystery. So then thealias
command shows my new alias. I finally type insudo 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 tochown
the folder, but still runinitdb
as my user rather thanroot
.UPDATE Running the command
sudo chown myuser /usr/local/var/postgres/
, and then runninginitdb
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. -
aardvarkk over 11 yearsI'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.
-
Lipongo over 11 yearsWhen 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.