Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to install postgresql in the latest ubunutu, and I'm following these instructions. When I sudo -u postgres psql postgres I get this error: could not change directory to "/home/james"

I'd appreciate any help.

share|improve this question
up vote 2 down vote accepted

It sounds like the user postgres, which you are using to run psql, may not read the directory you are executing the statement from (/home/james/)

  • sudo = s witch u ser and do - sw
  • u = "as user"
  • postgres = user to switch to
  • psql = command to execute
  • postgres = database to connect to

i'm guessing here, but i think sudo will open a new shell/environment in the current dir for postgres and execute psql there. If psql is not allowed to read the current dir, this error may occur.

try to switch the directory before executing the command:

cd /tmp
share|improve this answer
postgres user has some home directory ( refer /etc/passwd ), its better to change to that dir more than /tmp. – thegeek Jul 22 '10 at 8:55

I know this is old, but this question comes up highly-ranked on Google, and I prefer my answer. :)

Stick a -i in that sudo command - sudo also doesn't change the value of $HOME unless you do the -i command (which simulates an initial login). That also load the postgres user's .profile, etc. So, what you would run is:

sudo -i -u postgres psql postgres

Demo time!

sauer@pyro:~$ sudo -u postgres sh -c 'echo $HOME'
sauer@pyro:~$ sudo -i -u postgres sh -c 'echo $HOME'

Single quotes are important to that example, BTW. :)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.