postgres build from src in /usr/local/pgsql/ but after run sudo -i -u postgres psql sudo: unable to change directory to /var/lib/postgresql
Assistant
The error message you're seeing indicates that the postgres user (which is typically the user that runs the PostgreSQL server) is attempting to change to the directory, but it cannot because the directory does not exist or there are permission issues.
/var/lib/postgresql
Here are a few steps you can take to resolve this issue:
1. Create the Directory
If the directory /var/lib/postgresql doesn't exist, you can create it. Use the commands below:
sudo mkdir -p /var/lib/postgresql
2. Set Ownership
After creating the directory, ensure that the postgres user has ownership of it:
sudo chown postgres:postgres /var/lib/postgresql
3. Set Permissions
You may also want to ensure that the permissions are set correctly for the directory:
sudo chmod 700 /var/lib/postgresql
4. Check Existing Directories
If your PostgreSQL data directory is located elsewhere (for example, if you configured it to point to another directory during initialization), you might want to check your PostgreSQL configuration. You can find out where the data directory is pointing by checking the postgresql.conf file. It is usually located in your PostgreSQL install's data directory. You can find the configuration file in a standard path or provide a custom path if specified.
Look for the line in postgresql.conf that starts with:
data_directory = '/path/to/data_directory'
Adjust the ownership and permissions of that directory similarly if needed.
5. Initialize the Database (if not done already)
If this is a new setup and you haven't initialized the database yet, you may need to do so:
Now you should be able to access the PostgreSQL shell:
sudo -i -u postgres psql
Conclusion
Following these steps should help you resolve the "unable to change directory" issue. Make sure to adjust the steps based on your actual PostgreSQL installation and configuration. If you continue to have issues, please provide any additional details about your configuration or error messages.