15 hours ago
Hello Railway Support,
I am running a Docker-based backup service using PostgreSQL 17 and a persistent Railway volume.
My Dockerfile specifies USER postgres, but RAILWAY_RUN_UID is currently set to 0, causing the service to run as root.
Our application requires a non-root runtime for security. The diagnostic check confirmed that the persistent volume ownership does not match the runtime user.
What is the officially supported solution to:
- Run the application as a non-root user.
- Maintain read and write access to the persistent volume.
- Preserve all existing files and ownership-sensitive security markers.
- Ensure correct permissions after restarts and redeployments.
The service is currently safely disabled. I do not want to recreate or delete the volume, disable security checks, or lose existing data.
Please provide the recommended permanent configuration.
Thank you.
11 Replies
15 hours ago
Volumes are mounted as the root user. For images that run as a non-root user, the documented setting is RAILWAY_RUN_UID=0, as described in our volume docs. That is why your service runs as root while the volume is attached.
Once the container has started, any change to file ownership or to the runtime user is handled by your image's own startup process. The volume and its existing data stay as they are when you change the service's variables or redeploy.
Status changed to Awaiting User Response Railway • about 15 hours ago
Railway
Volumes are mounted as the `root` user. For images that run as a non-root user, the documented setting is `RAILWAY_RUN_UID=0`, as described in [our volume docs](https://docs.railway.com/volumes). That is why your service runs as root while the volume is attached. Once the container has started, any change to file ownership or to the runtime user is handled by your image's own startup process. The volume and its existing data stay as they are when you change the service's variables or redeploy.
15 hours ago
Thank you. Could a Railway engineer confirm the recommended startup configuration for initializing persistent volume permissions and then running the application as a non-root user, while preserving existing files and security markers across redeployments?
We need a safe, permanent solution without recreating the volume.
Status changed to Awaiting Railway Response Railway • about 15 hours ago
13 hours ago
This thread has been opened as a bounty so the community can help solve it.
Status changed to Open Railway • about 13 hours ago
13 hours ago
You can run chown on the files you want your user to own, either with one deploy that uses RAILWAY_RUN_UID=0 or from a root shell. Then delete RAILWAY_RUN_UID so the container starts as postgres every time, this can work because ownership changes made at runtime are written to the volume itself. You can also set up an entrypoint in your dockerfile to get this to work. There are a couple of other ways around this, like giving access through the group instead of the owner, but the one I mentioned is the most robust solution.
12 hours ago
Is the volume used for backup files only, or does it also hold PGDATA? That determines which directory needs to be writable.
For a backup-only volume, I’d keep RAILWAY_RUN_UID=0 and use a small runtime entrypoint. Once Railway mounts the volume, create a dedicated backup-output directory if it’s missing and set ownership only on that new directory. Then use exec gosu postgres ... to launch the backup process with the output path set to that directory. On later starts, check write access as postgres and report a clear error if it fails. This keeps ownership changes away from the existing files and markers.
If the volume also holds PGDATA, check the image’s entrypoint first. The official Postgres entrypoint can recursively change ownership under PGDATA when started as root. The exact mount path and PGDATA path would help determine whether the same setup fits.
mengxianying
Is the volume used for backup files only, or does it also hold PGDATA? That determines which directory needs to be writable. For a backup-only volume, I’d keep RAILWAY_RUN_UID=0 and use a small runtime entrypoint. Once Railway mounts the volume, create a dedicated backup-output directory if it’s missing and set ownership only on that new directory. Then use exec gosu postgres ... to launch the backup process with the output path set to that directory. On later starts, check write access as postgres and report a clear error if it fails. This keeps ownership changes away from the existing files and markers. If the volume also holds PGDATA, check the image’s entrypoint first. The official Postgres entrypoint can recursively change ownership under PGDATA when started as root. The exact mount path and PGDATA path would help determine whether the same setup fits.
12 hours ago
Thank you for the detailed explanation.
The PostgreSQL database is hosted externally on Supabase. The Railway volume is mounted at /mnt/omar-b2-once and is dedicated to persistent backup attempt markers, not PGDATA.
Our priority is to preserve all existing security markers and prevent duplicate backup attempts.
Would you recommend a dedicated entrypoint that initializes only the necessary directory permissions and then drops privileges using gosu postgres, while preserving all existing markers across redeployments?
12 hours ago
Yes, I’d use a small entrypoint for this. Keep RAILWAY_RUN_UID=0 for startup, check that /mnt/omar-b2-once is mounted, and give postgres ownership only of a new claims/ directory if you need one. Then start the backup script with exec gosu postgres …. Leave the existing markers alone.
Before starting a backup, the script should check the existing marker for that job. If it already records an attempt, skip it. If the script can’t check the marker, stop and log the error. For new attempts, use atomic mkdir to claim a job ID that stays the same across redeploys. Keep that claim even if the backup fails, so restarting the service can’t trigger another attempt.
In a local Docker test with synthetic markers, only one of two concurrent processes got the claim, and a fresh container using the same volume skipped it. Before re-enabling the service, I’d check that postgres can detect your existing marker with its current permissions.
mengxianying
Yes, I’d use a small entrypoint for this. Keep RAILWAY_RUN_UID=0 for startup, check that /mnt/omar-b2-once is mounted, and give postgres ownership only of a new claims/ directory if you need one. Then start the backup script with exec gosu postgres …. Leave the existing markers alone. Before starting a backup, the script should check the existing marker for that job. If it already records an attempt, skip it. If the script can’t check the marker, stop and log the error. For new attempts, use atomic mkdir to claim a job ID that stays the same across redeploys. Keep that claim even if the backup fails, so restarting the service can’t trigger another attempt. In a local Docker test with synthetic markers, only one of two concurrent processes got the claim, and a fresh container using the same volume skipped it. Before re-enabling the service, I’d check that postgres can detect your existing marker with its current permissions.
12 hours ago
Keep RAILWAY_RUN_UID=0 for startup. In the entrypoint, verify /mnt/omar-b2-once is mounted. If claims/ is missing, create it and give postgres ownership of that directory only. Then launch your backup script with exec gosu postgres /path/to/backup.sh.
In the backup script, check the existing marker first. Skip if it records an attempt. Otherwise, claim the job with an atomic mkdir under claims/, using the same job ID across redeploys. Skip if the claim exists, stop on permission errors, and keep the claim even if the backup fails.
Before re-enabling the service, run the marker check as postgres with the backup command disabled. Confirm the old marker triggers a skip. Then use a test job ID to confirm it gets claimed once and skipped after a redeploy.
mengxianying
Keep RAILWAY_RUN_UID=0 for startup. In the entrypoint, verify /mnt/omar-b2-once is mounted. If claims/ is missing, create it and give postgres ownership of that directory only. Then launch your backup script with exec gosu postgres /path/to/backup.sh. In the backup script, check the existing marker first. Skip if it records an attempt. Otherwise, claim the job with an atomic mkdir under claims/, using the same job ID across redeploys. Skip if the claim exists, stop on permission errors, and keep the claim even if the backup fails. Before re-enabling the service, run the marker check as postgres with the backup command disabled. Confirm the old marker triggers a skip. Then use a test job ID to confirm it gets claimed once and skipped after a redeploy.
12 hours ago
Thank you for your help. The proposed approach passed our local tests. We are now preparing the Railway implementation and will confirm the solution after verifying persistent claims across redeployments.
omaralsanaa
Thank you for your help. The proposed approach passed our local tests. We are now preparing the Railway implementation and will confirm the solution after verifying persistent claims across redeployments.
12 hours ago
OK
mengxianying
OK
5 hours ago
Thank you for your help. All seven local tests passed successfully.
Could you confirm the supported way to test persistent volume permissions on Railway without running the actual backup, while preserving existing claim markers and verifying that a consumed claim remains blocked after redeployment?
omaralsanaa
Thank you for your help. All seven local tests passed successfully. Could you confirm the supported way to test persistent volume permissions on Railway without running the actual backup, while preserving existing claim markers and verifying that a consumed claim remains blocked after redeployment?
4 hours ago
Use a temporary test mode in the same service, with the real backup disabled and RAILWAY_RUN_UID=0 kept for startup.
- After the volume is mounted, check that postgres can read the existing marker.
- As postgres, create a test claim under a separate test namespace using a fixed ID.
- Redeploy with the same volume and test ID. Confirm the existing marker is still readable and the test claim already exists.
- Treat permission or mount errors as failures, and remove only the test claim after verification.
If possible, run the test through your normal entrypoint so it also checks the switch to postgres.