Skip to content
sadat.
All writing
NOTES FROM THE WORKBENCH

Fixing Persistent Media Storage in Payload CMS and RAILWAY

How I fixed missing media files and investigated Payload CMS upload errors using Railway Volumes, SSH debugging, and persistent storage.

Fixing Payload Media Storage

Getting an application deployed successfully feels like reaching the finish line. But sometimes, that's exactly where the next challenge begins.

In my previous article, I shared how I debugged my Next.js portfolio deployment on Railway, resolved Payload CMS migration issues, and investigated unexpected memory usage.

After those issues were resolved, my portfolio was running properly. The homepage worked, the blog system was accessible, and I could manage content through Payload CMS.

Everything seemed ready.

Then I tried to upload images for my blog posts.

That's when I discovered another important production issue: my application could store information about uploaded images in PostgreSQL, but the actual image files were not guaranteed to survive a deployment.

This article covers how I investigated the problem, configured persistent storage using Railway Volumes, and debugged an unexpected upload failure along the way.

1. The Problem: My Blog Image Was Missing

My portfolio uses a combination of:

- Next.js 16 for the application.

- Payload CMS for managing blog posts and media.

- PostgreSQL for storing content and database records.

- Railway for hosting the application and database.

After publishing my first technical article, I noticed that its hero image wasn't loading correctly.

The browser console showed an image optimization error:


1GET /_next/image?... 400 (Bad Request)

Initially, I suspected the Next.js image optimizer.

I found that my image component was requesting a quality value that didn't match the allowed values in my Next.js configuration.

That was one issue, but fixing it wasn't enough.

When I inspected the server logs, I found something more important:

1ERROR: File debug-ram.jpg for collection
2media is missing on the disk.
3
4Expected path: E:\Dev\Sadat-Portfolio\public\media\debug-ram.jpg

The database still knew about the image, but the physical file was missing from the expected directory.

This helped me understand an important difference between storing a media record and storing the actual media file.

2. PostgreSQL Wasn't Storing My Image Files

At first, I assumed that because Payload CMS was connected to PostgreSQL, my uploaded files would also be preserved.

But my existing media configuration used local filesystem storage.

In Payload, the upload directory was configured using staticDir:

1staticDir: path.resolve(
2 dirname,
3 '../../public/media'
4),

With this setup, PostgreSQL maintains media-related information, such as filenames, metadata, and references.

The actual image bytes are written to the application's filesystem.

This works perfectly well in a local development environment.

However, production hosting introduces another consideration: the application's filesystem may not be persistent across deployments.

On Railway, files written to the normal application filesystem can disappear when a new deployment replaces the running container.

That explained how the CMS could retain an image record even when the corresponding file was no longer available.

My Temporary Solution

For my first article, I converted the missing hero image into an optimized WebP file and added it directly to my project's public/blog directory.

I then referenced that image from the article page.

This worked because the image became part of my application's Git-tracked source files and was included in the deployment.

It was a reasonable temporary solution for a single article.

But I didn't want to update source code and push a new Git commit every time I published a blog post.

I wanted to upload images directly through Payload CMS.

That required persistent storage.

3. Choosing Railway Volume

There are several ways to store uploaded media in a production application.

Some applications use object storage services such as Amazon S3 or Cloudflare R2.

For my personal portfolio, I decided to keep the existing PostgreSQL database and use a Railway Volume for persistent media files.

A Railway Volume provides persistent filesystem storage that can be mounted into an application container.

Instead of saving uploads to the deployment's temporary filesystem, the application can write them to the mounted volume.

The idea was straightforward:

Payload CMS → Upload Directory → Railway Volume → Persistent Files

I created a volume for my portfolio service and configured its mount path as:

1/app/media

The next step was telling Payload CMS to use that directory in production.

4. Connecting Payload CMS to the Volume

I updated the Media collection configuration in:

src/collections/Media.ts

The important change was:


1staticDir:
2 process.env.RAILWAY_VOLUME_MOUNT_PATH ||
3 path.resolve(dirname, '../../public/media'),
4

This configuration supports two environments.

Local Development

When the Railway-specific environment variable is unavailable, Payload uses the original local directory:

1public/media

Railway Production

When deployed with the Railway Volume mounted, Payload uses the path supplied by:

1RAILWAY_VOLUME_MOUNT_PATH

For my deployment, that path was:

1/app/media

I liked this approach because it allowed the application to maintain a single configuration without hardcoding a production-only directory everywhere.

After applying the change, I deployed the updated application.

I expected image uploads to work.

But the next test revealed another problem.

5. The Unexpected 400 Bad Request

I opened the live Payload admin panel and attempted to upload a small image.

Instead of saving successfully, the request failed.

Chrome DevTools showed:

1POST /api/media?depth=0&fallback-locale=null
2
3400 Bad Request

The response body was:

1{
2 "errors": [
3 {
4 "message": "There was a problem while uploading the file."
5 }
6 ]
7}

This message wasn't particularly helpful.

It confirmed that the upload failed, but it didn't identify the underlying cause.

I also checked Railway's deployment logs.

Unfortunately, the server reported the same generic error without exposing a useful filesystem exception.

At this point, several explanations were possible.

The Volume might not have been mounted at the expected path.

The application might not have had permission to write files.

The configured directory might have been incorrect.

Or the failure might have occurred somewhere else in Payload's upload-processing pipeline.

Instead of changing multiple settings blindly, I decided to investigate the runtime environment directly.

6. Debugging the Railway Container with SSH

I installed the Railway CLI and connected to the running application container using Railway SSH.

The first thing I checked was whether the media directory existed.

1ls -ld /app/media

The output was:

1drwxr-xr-x 3 root root 4096 /app/media

This confirmed that the directory existed.

It also showed that the directory belonged to the root user.

That raised another question: Was the application running with sufficient permissions?

Checking the User

I ran:

1id

The SSH session returned:

1uid=0(root) gid=0(root) groups=0(root)

The shell was running as root.

But checking the user alone wasn't enough. I still wanted to verify whether the mounted directory actually accepted writes.

Testing File Creation

I used a simple command:

1touch /app/media/test-write.txt

The command succeeded.

I then removed the temporary file:

1rm /app/media/test-write.txt

Both operations worked.

This proved that the SSH session could write to the mounted directory.

It didn't completely rule out application-level permission or upload-processing problems, but it eliminated one major possibility.

The important lesson was that a generic application error should not automatically be interpreted as a permission error.

A hypothesis needs to be tested.

7. Getting Media Uploads Working

After checking the mounted directory, reviewing the storage configuration, and redeploying the application, I tested the upload process again.

This time, media uploads through Payload CMS worked.

I could create media records and access the uploaded images through the application.

The working configuration continued to use the existing PostgreSQL database and the Railway Volume mounted at /app/media.

One detail is worth mentioning: the earlier server logs did not reveal the precise internal cause of the failed upload. Although the issue was resolved during configuration checks and redeployment, I cannot confidently attribute the failure to a single specific setting.

That distinction matters.

Finding a working solution and proving the exact root cause are not always the same thing.

How I Can Verify Persistence

The important final check for a persistent storage setup is not just whether an upload works.

It is whether the file remains accessible after the application is redeployed.

A practical verification process is:

1. Upload an image through Payload CMS.

2. Open its public media URL and verify that it loads.

3. Redeploy the Railway application.

4. Open the exact same URL again.

5. Confirm that the original file remains available.

This test validates the persistence behavior that motivated the Volume setup in the first place.

8. What I Learned About Production Storage

This experience taught me several practical engineering lessons.

A Database Record Is Not the Same as a File

A database can contain a valid media record while the file it references is missing.

Applications need to account for both metadata storage and binary file storage.

Local Filesystems and Persistent Storage Are Different

A folder that works locally may not behave the same way inside a deployed container.

Understanding the hosting environment is part of building a reliable application.

Deployment Success Doesn't Guarantee Feature Success

My application could deploy and serve pages successfully while a specific feature—media uploads—still failed.

Critical workflows need to be tested after deployment.

Logs Don't Always Reveal the Root Cause

The 400 Bad Request response identified the failing operation but provided very little explanation.

Direct inspection through SSH helped me investigate the actual runtime environment.

Small Tests Can Eliminate Big Assumptions

Commands such as id, ls, and touch may seem simple, but they can be extremely useful when investigating filesystem problems.

Rather than immediately modifying permissions or rebuilding the application, I could verify the environment first.

9. What I Would Improve Next

The Railway Volume approach is sufficient for my current portfolio requirements, but there are still areas worth improving.

First, I want to make file upload failures easier to diagnose through better error logging.

Second, I want to ensure that database records and stored media files remain consistent as the application grows.

Third, I want to establish a reliable backup and recovery process for uploaded content.

Persistent storage protects files across deployments, but it should not be confused with a backup system.

Finally, if the project eventually needs distributed storage or multiple independent application instances, I may revisit an object storage solution.

For now, my goal is to keep the architecture simple, maintainable, and appropriate for the project's scale.

Conclusion

What began as a missing blog image turned into a practical lesson about production storage.

I learned how Payload CMS separates media metadata from physical files, why container filesystems require special consideration, and how Railway Volumes can be integrated into an existing Next.js application.

More importantly, I gained experience debugging a real production problem by inspecting logs, checking the runtime environment, testing filesystem access, and validating application behavior.

The biggest takeaway from this experience is simple:

A feature working locally does not mean its storage and deployment assumptions are correct for production.

Building reliable software requires understanding not just the application code, but also the environment in which that code runs.

This is exactly why I enjoy documenting these experiences. Every unexpected issue gives me another opportunity to understand how real systems behave.


GitHub: https://github.com/rafi1210

Portfolio: https://sadatahmedrafi.xyz/