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.