I have a Postgres database on Heroku and I want to have that database hosted on ElephantSQL, but I cannot for the life of me find how to do it. I have a DUMP file downloaded from the Heroku Postgres database, but I cannot find how I put the data into ElephantSQL.
My database was linked to a Django app and I already have my instance on ElephantSQL linked to the a copy of the same Django app, but hosted on Render. I can see on ElephantSQL that the instance has all the tables, so I just need to put the data in there somehow.
Any tips or hints are welcome!
2
Answers
The tables that you already see may be the problem. It may also be that the parents of those tables are the problem. An SQL dump is just a series of SQL commands that run in order to write schema and data back into a new database.
The first SQL commands set up the databases, schemas, and tables. If your tables are already there, your import may be failing because it wants to create new tables that already exist. Sorry without more information on the specific errors you are seeing, it’s hard to be more specific. Hopefully one of these 4 options help you. Let me know in the comments how it goes.
For all of the options below, I would suggest backing up your destination server database to be sure that you don’t mess up the Django app that currently is working there even without your data.
Option 1: Look for a way to export your source database without database, schema, and table definitions.
Option 2: If you’re careful, you can edit your dump file to remove those initial setup commands, and have the dump file start with the commands that only push the data to your tables.
Option 3: (IMPORTANT to backup the destination server first for this one.) Drop the database, schema, and/or tables on the destination server so that nothing is pre-defined, and see if your dump file can reconstruct everything again the way Django needs it on the destination server with your data.
Option 4: It may be a lot of tables, but you can usually export individual table data files with an option to not include table definitions. Then you have to import all the tables as separate imports. A mix of option 3 and 4 may work also where if you can’t find an option to not include the definition, drop the tables at the destination and import each table independently.
Here is what worked for me using a Mac terminal:
pg_dump -h heroku_host -d heroku_database -U heroku_user -p heroku_port -W -Ft > latest.dump
You’ll be prompted to input the password from Heroku PostgreSQL. Note that these
heroku_xxx
are found in the Heroku PostgreSQL credentials.pg_restore -h elephant_host -d elephant_database -U elephant_user -p 5432 -W -Ft latest.dump
You’ll be prompted to input the password from Heroku PostgreSQL. Note that these
elephant_xxx
are found in the ElephantSQL credentials and that they use the term "server" instead of host.