Importing table dump to Heroku Postgres - ruby-on-rails

I've created a dump file of one table on my local db using pg_dump and want to import it to my production db on Heroku to replace the current production version of the table.
I was looking at these instructions: https://devcenter.heroku.com/articles/heroku-postgres-import-export#import-to-heroku-postgres
Should I use pg_restore or can I use heroku pg:backups:restore? Does using heroku pg:backups:restore drop the entire db and replace it with the contents of the dump file or will it only drop and replace what is in the dump file? I am concerned about data other than what's in the dump file being dropped (since my dump file is only one table). I will of course create a backup of production before doing this, but am curious what the best approach is. Thanks!

Related

Rails - importing Postgres production database to local development database

I have a database dump named dump.sql from production DB and when I try to import it to my local development DB I get an error
ERROR: role "petdoctors_prod" does not exist
\connect: FATAL: database "petdoctors_prod" does not exist
It looks like the name of the production database is petdoctors_prod and my local development db is called petdoctors_development. Is it possible to import prod DB to dev?
What's the best way of dumping the db from prod?
The following steps should help in importing the dump.sql to your local machine:
Run rake db:drop from on your local machine to drop the existing database
Run rake db:create. This would create the database mentioned in your database.yml file as per the environment. In this case, it would create a database called petdoctors_development
Import the dump to petdoctors_development. This syntax of this step would vary depending on the database you're using. For example, to import a dump in PostgreSQL, we use psql <db_name> < <Path to dump file>
Run rake db:migrate, in order to run all the migrations in your app
Post this, the app will run on development, with the same data as that of the dump taken from production.

Duplicating Ruby on Rails 5 project with PostgreSQL database

I have a Project-A, and I'm starting Project-B. I want to use Project-A as a starting point. So I copied the files, but how can I duplicate the database? Thank you!
The exact command depends on what type of database you are copying from and too, also on whether you want to copy the structure only or the structure and the content.
A general way to do this would be to export the Project-A database into an SQL file, then run that SQL file through the project-B database. The SQL file can store the structure, or the content or both - you choose when you do the export.
Postgresql uses the command pg_dump to export to SQL. The accepted answer in the question linked to in jdgray's comment shows how the output of pg_dump can be piped directly into the second database so that no intermediate file is created.
To get your database
pg_dump -Fc mydb > db.dump
To restore it:
pg_restore -d <you_new_db_name> /db.dump
This is assuming you are going from pg to pg. All data and structure and relationships will come over with this. I would suggect using pgadmin4 to make the new db before hand so you can just import over to it. In your database.yml change the db name.
If you need addition stuff, like declaring which ip address your db is on use the -p flag. Here is the link to more flags (Postgres v 9.6):
Postgres Link
I just edited the db name from database.yml and ran rake db:create db:migrate

How to create dump file from Cloud 9 PostgreSQL database?

I am now working on Cloud 9 and need to see my PostgreSQL database. So I hope to know the below two methods.
How to create dump file from PostgreSQL database when you know database name in Cloud 9?
Which tool can I use to see the tables of PostgreSQL database dump?
Use pg_dump. It looks like Cloud9 doesn't permit remote PostgreSQL connections, so you'll need to run it within the workspace, then transfer the dump file to your computer. Use pg_dump -Fc to make a PostgreSQL-custom-format dump, which is the recommended format.
To list tables in the dump use pg_restore -l to list the dump's table of contents. Generally it's easier to work with dumps by restoring them to a local PostgreSQL install (again, using pg_restore) though.
For more detail see the PostgreSQL manual

synching two databases in heroku rails app

I have crm rails application deployed in heroku so my database is postgresql. I have an iphone app which is using mysql database. Now what i need to do is to synch my iphone database with my heroku database.
Can anyone please help me on this is it possible to sync two different databases in heroku
Thanks in Advance
The way we did this was to do it manually
Yep, it's a real pain, but at least you know it's done correctly
--
Process
For migrating from Postgres to MYSQL is as follows:
"Backup" the database using pg_backups
Download the backup locally
In MYSQL (we used PHPMyAdmin), create a series of SQL calls to insert the respective data into the correct tables
Migrating from MYSQL to Postgres is, as much as I can determine, a similar process:
Download the SQL dumps of your MYSQL DB (I only know how to do this in PHPMyAdmin)
Configure those files to be "PG" ready
Send the SQL to your Heroku postgres db's
--
Notes
For specifics, I only have the following:
If you dump from Heroku, you'll not get a traditional SQL file; you'll receive a .dump file. To change this, you'll need to use pg_restore in the following way:
$ cd c:/your/postgres/install/pg_restore/location
$ pg_restore path_to_your_dump.dump > path_to_your_sql.sql
This will give you the ability to "read" the file, uploading to your db & importing as you wish. That's when the fun begins :)
Importing MYSQL
The way to import MYSQL into a PG db will have to be done manually
Although we've never done this directly (we only migrated from PG), there are several things you can do. Firstly, you can use the PG2MYSQL converter to give you an idea as to the syntax differences between MYSQL & PG
Then I would perform a "dump" from MYSQL in SQL format. This will give me the data required for PG. This will allow me to insert into my PG database (you might have to do this locally to give you the ability to determine the syntax), from which you can then patch in the MYSQL data
I understand it's not the most elaborate explanation - I hope it gives you some ideas

How to load the data from a .sqlite3 file to a Ruby on Rails Application

I have a production.sqlite3 file which I want to import its data to the current rails project, the database schema is match between the file and my current project. I did copy the content to the development.sqlite3 file but this does not work. The only way I know to insert data to the database is by loading some yml file or use the seed command. Is there any magic command or other ways to let the rails loading the data from .sqlite file? Because from what I disover, the behavior of rails is that it only creates the .sqlite3 file when you do rake db:create &migrate, but it will ignore what ever manual changes happen in that file. Please help!
It looks like renaming your .sqlite3 file should work. Make sure you have restarted your server, and that the server is running in the correct environment. If you move both the .sqlite3 files into a completely different folder, and restart the server, an error should occur. If it doesn't, your server didn't properly restart - kill all ruby processes and try again.
In answer to your question about migrating data, I had a similar problem recently, where I had to migrate from sqlite3 to mysql. Best solution I found was yaml_db. It seemed to do the job beautifully - add it to the project, then do
rake db:data:dump
Swap the database configurations (or migrate a new database file, or change your development environment, whatever it is you need to do to make an empty but structured database active), then:
rake db:data:load

Resources