Build a Database Service

Railway's platform primitives allow you to build any type of service your system requires, including database services. This guide aims to guide you through the essential features to build your own database service.

For the purpose of this guide, we will use the official Postgres image as an example.

Service Source

As discussed in the Services guide, a crucial step in creating a service is defining a source from which to deploy.

To deploy the official Postgres image, we'll simply enter postgres into the Source Image field:

Screenshot of a Docker image source


Attach a volume to any service, to keep your data safe between deployments. For the Postgres image, the default mount path is /var/lib/postgresql/data.

Just attach a volume to the service you created, at the mount path:

Screenshot of a mount path

Environment Variables

Now, all you need to do is configure the appropriate environment variables to let the Postgres image know how to run:

Screenshot of environment variables

Note the DATABASE_URL is configured with TCP Proxy variables, but you can also connect to the database service over the private network. More information below.


Private Network

To connect to your database service from other services in your project, you can use the private network. For a postgres database service listening on port 5432, you can use a connection string like this -

TCP Proxy

If you'd like to expose the database over the public network, you'll need to set up a TCP Proxy, to proxy public traffic to the Postgres port 5432:

Screenshot of TCP proxy configuration


That's all it takes to spin up a Postgres service in Railway directly from the official Postgres image. Hopefully this gives you enough direction to feel comfortable exploring other database images to deploy.

Remember you can also deploy from a Dockerfile which would generally involve the same features and steps. For an example of a Dockerfile that builds a custom image with the official Postgres image as base, take a look at Railway's SSL-enabled Postgres image repo.

Template Marketplace

Need inspiration or looking for a specific database? Our Template Marketplace already includes solutions for many different database services. You might even find a template for the database you need!

Here are some suggestions to check out -

Edit this file on GitHub