Backup a Vercel Postgres Database

Apr 19, 2024

Vercel Postgres provides a quick and easy way to spin up a new database. In my opinion, it provides a perfect level of abstraction that allows you to quickly create a database and focus primarily on building your application.

The database section within the Vercel platform doesn't provide a way to backup a database. The process is slightly more involved than simply clicking a few buttons.

To get started, make sure you have Docker installed on your computer. I use a M2 Macbook Air, so as you read this, please keep that in mind.

Next, copy two values from the Vercel Postgres dashboard and save them locally to use them later. We'll need POSTGRES_HOST and POSTGRES_PASSWORD.

Create a directory somewhere on your machine called db-backup. cd into this directory. With Docker installed, we're going to run the following command:

docker run -it --rm -v $(pwd):/backup postgres:15.4 sh -c "pg_dump -h POSTGRES_HOST -p 5432 -U default -d verceldb > /backup/backup.sql"

Make sure to replace POSTGRES_HOST with your actual host variable. You will be prompted to input the database password. Enter the password you saved from Vercel previously. After a few moments, you should see a backup.sql file in your db-backup directory.

What is this doing?

We are mounting the current directory as /backup and connecting to the database. We run pg_dump and output the file as backup.sql. One thing to note here is that we're setting the version of Postgres to be 15.4. This is the current version of Vercel Postgre. I am sure this could change at some point.

It's as simple as that. If you really want to get fancy, you could even run a CRON job that can automate this whole process to have automatic backups. It's always a good idea to frequently backup your database just in case.