Problem
Restoring Postgres databases in Fly.io is super easy; we just need to run the below command in the Fly console as mentioned in the Fly.io Heroku migration documentation.
But sometimes we might get a version mismatch error because the fly DB Postgres version and the Postgres DB version that host in the other service are different.
Workaround
As a workaround to fix this version mismatch issue, instead of running pg_dump and pg_restore in the web app, we can take a DB dump and restore it to Fly DB from the local environment.
In the below steps, I explain how to restore the DB dump in the Fly database.
If Postgres app is already there in Fly.io, skip to Step 2.
1. Create a Postgres DB
Create a Postgres DB on the fly by running the following command:
E.g.,
Output be something like this,
Store the Postgres cluster credentials somewhere, we will use these credentials later.
2. Connect to a remote Postgres DB
In this case,
This will establish a proxy forwarding to the localhost on port 5434.
3. Create a new DB
Skip this step if Postgres cluster already has a database with the app’s name.
In a new terminal, run the following command:
In this case,
Enter the password (from the credentials) then, in the psql console, run the following command:
E.g.,
This will create the DB and ensure that the DB name is the same as the app name.
4. Restore Dump to DB
Open a terminal in the path where the PG dump file is saved and run the following command:
E.g.,
This will restore the records in the database.
5. Attaching a Fly app
Skip this step if the app is attached to a Postgres cluster.
Ensure that the fly web app doesn’t have a secret key with the name DATABASE_URL
, if a secret with that name exists, then unset that secret by running the following command:
In this case,
This will remove the DATABASE_URL
secret from the web app.
To attach the fly app to the Postgres cluster, run the following command:
In this case,
Running this command will add DATABASE_URL
secret to the web app.
Now, when we access the site we can see all the data there, and we can also verify the records from the rails console.