When migrating an app from Heroku to AWS, you should take into account the following steps:
-
Uncomment the .ebextensions files. Delete the 01_cron.config file if you don't schedule jobs with whenever. (Sidekiq Scheduler gem is recommended, but if you already started using whenever just uncomment the lines in this file an keep it).
-
(OPTIONAL If you use only one DB with different Schemas)
Add this line in the config/database.yml file
production:
...
schema_search_path: 'desired_schema'
-
Copy the environment variables from Heroku to AWS
-
Commit and Deploy
- Create and download a backup from the Heroku Postgres database (It may take some minutes depending of the database size)
pg_dump HEROKU_DATABASE_URL -f dump.sql
Where HEROKU_DATABASE_URL is a String with the format:
postgres://user::password@HOST:port/database_name
If it throws you an error like this:
pg_dump: server version: 9.X.X; pg_dump version: 9.X.X
pg_dump: aborting because of server version mismatch
Install the newest version of postgres with:
sudo apt-get install postgresql
or
sudo apt-get install postgresql-9.X
for the specific version and make the new link:
sudo ln -s /usr/lib/postgresql/9.X/bin/pg_dump /usr/bin/pg_dump --force
- (OPTIONAL If you use only one DB with different Schemas)
psql RDS_DATABASE_URL
CREATE SCHEMA desired_schema;
Where RDS_DATABASE_URL is a String with the format:
postgres://user::password@HOST:port/database_name
Use \dn to see the existing schemas
Then open the dump.sql
file and replace "public" with "desired_schema" everywhere.
- Restore the database in AWS
psql RDS_DATABASE_URL -f dump.sql
Ignore the errors of existing role or relations.