Can I start a new Postgres service with an existing volume?
ericg
HOBBYOP

6 months ago

I launched a Postgres instance and then later deleted the service but retained the volume in my project.

Is it possible to launch a new Postgres instance and have it mount an existing volume?

What would the workflow be to reuse a volume? Thank you.

Solved$10 Bounty

11 Replies

testuser123
PRO

6 months ago

No, Railway doesn’t let you attach an old volume to a new Postgres service. Volumes are tied to the service they were created with. If you want to reuse the data, you’ll need to spin up a new Postgres service and restore from a backup or dump, not by reattaching the volume.

Just delete the old volume and create a new postgres service. It'll come up with a new volume. If you have nothing in that volume, you can just delete it.


Yeah. Sure you can.

Instead of selecting Database > Postgres, select Docker Image. Just type in ghcr.io/railwayapp-templates/postgres-ssl:16 .

Once you've got that service, mount the volume. Easy.

To do this correctly however, you're going to want to copy paste the environmental variables from the Postgres template before deploying this image. I've attached the default values below for your convenience. Make sure you mount your old volume at /var/lib/postgresql/data.

Make sure to do this before you deploy for the first time.

DATABASE_PUBLIC_URL="postgresql://${{PGUSER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{PGDATABASE}}"
DATABASE_URL="postgresql://${{PGUSER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:5432/${{PGDATABASE}}"
PGDATA="/var/lib/postgresql/data/pgdata"
PGDATABASE="${{POSTGRES_DB}}"
PGHOST="${{RAILWAY_PRIVATE_DOMAIN}}"
PGPASSWORD="${{POSTGRES_PASSWORD}}"
PGPORT="5432"
PGUSER="${{POSTGRES_USER}}"
POSTGRES_DB="railway"
POSTGRES_PASSWORD=""
POSTGRES_USER="postgres"
RAILWAY_DEPLOYMENT_DRAINING_SECONDS="60"
SSL_CERT_DAYS="820"

erisa
HOBBY

6 months ago

You can also create a new postgres service, remove the volume it attaches by default and then attach your old volume at the same path. But since the option for postgres only creates a service like any other, the result will be essentially the same as what loudbook posted.


erisa

You can also create a new postgres service, remove the volume it attaches by default and then attach your old volume at the same path. But since the option for postgres only creates a service like any other, the result will be essentially the same as what loudbook posted.

I was actually not able to remove it without deleting the service itself.


samgordon

I was actually not able to remove it without deleting the service itself.

erisa
HOBBY

6 months ago

It seems you're right! I definitely remember this was possible through the right click menu before (you can see what I mean on a non-database service) since I did it while manually migrating some databases to Metal before that was automatic, but it's not possible for the database ones anymore. Not sure what's up with that, but you're totally right!


samgordon

Yeah. Sure you can.Instead of selecting Database > Postgres, select Docker Image. Just type in ghcr.io/railwayapp-templates/postgres-ssl:16 .Once you've got that service, mount the volume. Easy.To do this correctly however, you're going to want to copy paste the environmental variables from the Postgres template before deploying this image. I've attached the default values below for your convenience. Make sure you mount your old volume at /var/lib/postgresql/data.Make sure to do this before you deploy for the first time.DATABASE_PUBLIC_URL="postgresql://${{PGUSER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{PGDATABASE}}" DATABASE_URL="postgresql://${{PGUSER}}:${{POSTGRES_PASSWORD}}@${{RAILWAY_PRIVATE_DOMAIN}}:5432/${{PGDATABASE}}" PGDATA="/var/lib/postgresql/data/pgdata" PGDATABASE="${{POSTGRES_DB}}" PGHOST="${{RAILWAY_PRIVATE_DOMAIN}}" PGPASSWORD="${{POSTGRES_PASSWORD}}" PGPORT="5432" PGUSER="${{POSTGRES_USER}}" POSTGRES_DB="railway" POSTGRES_PASSWORD="" POSTGRES_USER="postgres" RAILWAY_DEPLOYMENT_DRAINING_SECONDS="60" SSL_CERT_DAYS="820"

ericg
HOBBYOP

6 months ago

I apprecate the great response. Attempted to do this and the service did come up with the old volume but I could not authenticate. (I added a TCP proxy as well.) In the env vars, do I need to put the correct "old" password for POSTGRES_PASSWORD or leave it blank (or create a new one)?


ericg

I apprecate the great response. Attempted to do this and the service did come up with the old volume but I could not authenticate. (I added a TCP proxy as well.) In the env vars, do I need to put the correct "old" password for POSTGRES_PASSWORD or leave it blank (or create a new one)?

Yeah, this was a potential issue. You're going to want to replicate the exact same configuration as the old one. If you can remember it.

There's always the option of SSHing in and dealing with this manually, too. You'll likely need the old password.


If this isn't an option for you, there is definitely a way to extract the data without the original login. It will take a bit of effort though.


ericg
HOBBYOP

6 months ago

Ok, looks like I might be in business! I did use this container: ghcr.io/railwayapp-templates/postgres-ssl:16
Password had to match the original DB.
I'll keep looking at it, but I believe this is a feasible workflow. Thanks loudbrook!


ericg

Ok, looks like I might be in business! I did use this container: ghcr.io/railwayapp-templates/postgres-ssl:16Password had to match the original DB.I'll keep looking at it, but I believe this is a feasible workflow. Thanks loudbrook!

You’re welcome


Status changed to Solved uxuz 6 months ago


erisa

It seems you're right! I definitely remember this was possible through the right click menu before (you can see what I mean on a non-database service) since I did it while manually migrating some databases to Metal before that was automatic, but it's not possible for the database ones anymore. Not sure what's up with that, but you're totally right!

brody
EMPLOYEE

6 months ago

People kept deleting volumes from Postgres and losing data, so our database templates now have a requirement for the volume that prevents you from deleting the volume, or mounting it to the wrong location.


Status changed to Awaiting User Response Railway 6 months ago


Status changed to Solved brody 6 months ago


Loading...