Ubuntu 22.04 - PostgreSQL Service scenarios to Start with 'FATAL: data directory is not accessible'
Could someone explain I'm wondering if anyone has experience with I'm experiencing an scenario with starting the PostgreSQL service on my Ubuntu 22.04 server. After installing PostgreSQL 14, I attempted to start the service using `sudo systemctl start postgresql`, but I received the following behavior in the logs: ``` Mar 12 14:23:45 myserver systemd[1]: Starting PostgreSQL RDBMS... Mar 12 14:23:45 myserver postgres[1234]: FATAL: data directory "/var/lib/postgresql/14/main" is not accessible: Permission denied Mar 12 14:23:45 myserver systemd[1]: postgresql.service: Control process exited, code=exited status=1 Mar 12 14:23:45 myserver systemd[1]: postgresql.service: Failed with result 'exit-code'. ``` I checked the permissions on the data directory and they seem correct: ```bash ls -ld /var/lib/postgresql/14/main ``` Output: ``` drwx------ 19 postgres postgres 4096 Mar 12 14:20 /var/lib/postgresql/14/main ``` The ownership is set to `postgres:postgres`, and the permissions allow read, write, and execute for the owner only. I also verified that the service is running as the `postgres` user. However, when I attempt to run the PostgreSQL command manually, I can access the directory without any issues: ```bash sudo -u postgres psql -c 'SELECT version();' ``` I suspect this might be related to AppArmor, as I noticed the following entry in the syslog: ``` Mar 12 14:23:45 myserver kernel: [ 5.789012] audit: type=1400 audit(1678620225.789:10): apparmor="DENIED" operation="open" profile="postgresql" name="/var/lib/postgresql/14/main" pid=1234 comm="postgres" requested_mask="r" denied_mask="r" fsuid=120 ouid=120 ``` I've tried reloading the AppArmor profiles with `sudo systemctl reload apparmor`, but the scenario continues. I also attempted to disable AppArmor for PostgreSQL entirely using: ```bash sudo ln -s /etc/apparmor.d/usr.sbin.postgresql /etc/apparmor.d/disable/ sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.postgresql ``` After doing this, I still need to start the PostgreSQL service. What are the steps I should take to resolve this scenario? Is there a better approach to handling permissions for PostgreSQL on Ubuntu with AppArmor? Any guidance would be greatly appreciated. How would you solve this? For context: I'm using Bash on Linux. Any ideas how to fix this?