Postgresql – Cannot start psql with user postgres “Could not change directory to /home/user ”

permissionspostgresqlpsql

I did a silly mistake. I wanted to set postgres permissions to a folder for exporting data from my database. So I set something like sudo chmod 777 -u postgres /home/user and then my home partition only has root permissions and nothing worked without root permissions. I solved this problem by using sudo chown user /home/user and then I set the permissions manually in the file manager on linux. It was very stupid, I didn't knew very much about allocating permissions to directories and users.

The Problem is now, that I can't connect to my database anymore.

 ~ $ sudo -u postgres psql
[sudo] password for user: 
could not change directory to "/home/user"
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I think I want to connect in a folder where the postgres user can't read.

Restarting the server with sudo service postgresql restart doesn't work as well.

I'm using PostgreSQL 9.1.

Best Answer

I solved the problem with the help of a relative.

The first problem was that my /home and /home/user folder didn't have the right permissions.

We set them correctly with chmod og+rX /home /home/user.

Then I forgot, that I created a new PGDATA directory in /home/user. And this directory had the wrong permissions. So we set them again for the user postgres with chown postgres:postrgres -R /home/user/PGDATA.

I decided to place the PGDATA directory in /home/user because of huge log-files that where produced during raster data queries with my PostGIS/PostgreSQL database. Sometimes I'm using Sublime Text 2 for queries and I quit them without stopping the database process and then the logfile became larger and larger and I was running out of space and nothing worked anymore.