How to create dump file from Cloud 9 PostgreSQL database? - ruby-on-rails

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

Related

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

Importing table dump to Heroku Postgres

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!

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 replicate data from development for test

I am running a rails 4 application with a large amount of data in my development db (postgres with postgis). In order to write tests I need the data from the development db.
Unfortunately I can not uses a loader/seed script since the data are coming from an api with quota limit. So it takes quite some time to collect the data, which is not wanted for test.
Therefore I was wondering how would it be possible to replicate data from the development db for testing?
Best,
Phil
Rails is just a framework - it doesn't have much to do with your actual data:
Every feature built into Rails is designed to help facilitate a more
flexible & smooth input of data into your system. This means that if
you want to transfer a large body of data for a Rails app, it will
likely be more efficient to do it at db level
We've migrated many db's before, and it's just a case of downloading an SQL file & then passing into the new db table. This varies for different SQL variants, but if you want to use Postgres, you'll be best using pg_dump & pgsql to restore:
pg_dump dbname > outfile
psql dbname < infile
I achieved this by creating a Bash script and using a database dump over ssh. The dump file is just text with create or replace statements, my example below is using MySql and I had to use sed to convert the escape characters. I am sure you could do something similar with Postgres. As part of my backup I also have to capture user maintained images which I do using rsync. I am loading the database into development but it could also be test.sqlite3 or any other database.
# Get database
ssh root#mywebsite.com \
'mysqldump -t --replace --compact --extended-insert=FALSE -u root -pDBPassword db_name' | \
sed "s/\\\\'/''/g" | sqlite3 ~/websites/mywebsite/db/development.sqlite3
# Get images
rsync -z root#mywebsite,com:/var/www/mywebsite/public/images/*.jpg \
~/websites/mywebsite/public/images/

Rails copy identical app on postgres - data not loading

We have an existing Rails 3 app that has been copied and loaded on a separate server. We've setup the posgres DB for this server; and also configured the database.yaml, pg gems, etc to setup for the port.
However, only the database schema can be migrate...though all the data files has the correct content.
I've tried variations of the db migrate, dump, resets, load, etc. But I'm not success getting the actual data in the database. Again, the server migration is for identical hardware/software config. So, its Rails3.1/Postgres9/Ruby 1.92
I don't get any errors, the data doesn't populate. The ultimate goal is to have an identical app on the 2 servers.
Any ideas? I've already spent 4 days fighting. Many thanks!!
"...the actual data in the database"
If you have an existing database with transactional data - then I think you want to use postgres tools to move the database? maybe I am not understanding the question correctly?
on the source machine
pg_dump DATABASE_NAME > ~/DATABASE_NAME_dump.sql
copy the dump file to the target machine
on the target machine
bundle exec rake db:create
psql DATABASE_NAME < ~/DATABASE_NAME_dump.sql
lots of good information here - http://www.postgresql.org/docs/9.0/static/backup.html
Have you tried the taps gem?
It enables you to transfer schema and data from one instance to another.

Resources