Problem
Restoring Postgres database in Fly.io is super easy we just need to run the below command in the fly console as mentioned in fly.io Heroku migration documentation
but sometimes we might get a version mismatch error as the fly DB postgres version and the postgres DB version that host in the other service is different.
Workaround
As a workaround to fix this version mismatch issue, instead of running the pg_dump and pg_restore in the postgres app we can take a DB dump and restore it to Fly DB from the local environment.
In the below steps, I explained how to restore the DB dump in Fly database.
If you already have a Postgres app in Fly.io skip to Step 2
1. Create Postgres DB
Create a postgres DB in 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 remote Postgres DB
In this case
This will establish a proxy forwarding to the localhost in port 5434.
3. Create a new DB
Skip this step if Postgres cluster already has database with app’s name.
In a new terminal run the following command,
In this case
Enter the password(from the credentials) then in 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 you have the PG dump file and run the following command,
E.g.,
This will restore the records in the DB.
5. Attaching a Fly app
Skip this step if the app is attached to Postgres cluster.
Ensure that the fly web app doesn’t have a secret key with the name DATABASE_URL
, if a secret with a 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 postgres cluster run the following command,
in this case,
Running this command will add DATABASE_URL
secret to the web app.
Now when you access the site you see all the data there and you can also verify the records from rails console.