@@ -74,51 +74,23 @@ func CheckDirectoryPermissions(path string) (*PermissionCheckResult, error) {
7474 // Check if directory is writable
7575 result .Writable = info .Mode ().Perm ()& 0200 != 0 || result .OwnerUID == currentUID
7676
77- // Check for permission mismatches
78- if result .OwnerUID != 999 || result .OwnerGID != 999 {
79- result .ErrorMsg = fmt .Sprintf (
80- "directory is owned by UID %d:GID %d, expected postgres user (999:999)" ,
81- result .OwnerUID ,
82- result .OwnerGID ,
83- )
84-
85- // Generate fix commands
77+ if ! result .Readable || ! result .Writable {
78+ result .ErrorMsg = fmt .Sprintf ("directory permissions are too restrictive: readable=%t, writable=%t" ,
79+ result .Readable , result .Writable )
80+
81+ // Suggest fix commands
8682 if currentUID == 0 {
8783 result .FixCommands = []string {
8884 fmt .Sprintf ("chown -R postgres:postgres %s" , path ),
89- }
90- } else if currentUID == result .OwnerUID {
91- // User owns the directory but needs to run as root to fix
92- result .FixCommands = []string {
93- fmt .Sprintf ("# Directory is owned by current user (UID %d), not postgres (999)" , currentUID ),
94- fmt .Sprintf ("# Option 1: Run container as root to fix permissions, then restart as postgres:" ),
95- fmt .Sprintf ("docker run --user root -v <volume>:/var/lib/postgresql/data <image>" ),
96- fmt .Sprintf ("" ),
97- fmt .Sprintf ("# Option 2: Fix permissions on host (if using bind mount):" ),
98- fmt .Sprintf ("sudo chown -R 999:999 %s" , path ),
99- fmt .Sprintf ("" ),
100- fmt .Sprintf ("# Option 3: Use named volume (Docker handles permissions automatically):" ),
101- fmt .Sprintf ("docker run -v pgdata:/var/lib/postgresql/data <image>" ),
85+ fmt .Sprintf ("chmod -R u+rw %s" , path ),
10286 }
10387 } else {
10488 result .FixCommands = []string {
105- fmt .Sprintf ("# Directory is owned by UID %d, not postgres (999) or current user (%d)" , result .OwnerUID , currentUID ),
106- fmt .Sprintf ("# Option 1: Run container explicitly as root to fix permissions:" ),
107- fmt .Sprintf ("docker run --user root -v <volume>:/var/lib/postgresql/data <image>" ),
108- fmt .Sprintf ("" ),
109- fmt .Sprintf ("# Option 2: Fix permissions from host:" ),
110- fmt .Sprintf ("docker run --rm --user root -v <volume>:/data alpine chown -R 999:999 /data" ),
89+ fmt .Sprintf ("# Run as root to fix permissions:" ),
90+ fmt .Sprintf ("sudo chown -R 999:999 %s" , path ),
91+ fmt .Sprintf ("sudo chmod -R u+rw %s" , path ),
11192 }
11293 }
113- } else if currentUID != 999 {
114- result .ErrorMsg = fmt .Sprintf (
115- "running as UID %d, but directory is owned by postgres (999). Container should run as postgres user" ,
116- currentUID ,
117- )
118- result .FixCommands = []string {
119- "# Container is running as wrong user" ,
120- "# Remove --user flag from docker run command to use default (postgres) user" ,
121- }
12294 }
12395
12496 return result , nil
0 commit comments