I have an existing database on my server containing many tables with content. Now I have created a new database but some columns are added.
Is it possible to migrate all the data from the one database to the other.
Kind regards.
I've used the yaml_db gem to migrate DBs: https://github.com/ludicast/yaml_db - this gem adds some rake tasks that are helpful
After installing the gem, you can run rake db:data:dump to save your database to a .yml file.
Then, after changing your database configuration, you can run rake db:data:load to load the data into your new database.
I like your answer! But a more easy way is to dump the whole database like you said. But just transfer it to another server.
Like this:
To Dump:
pg_dump -U demo02 -h localhost -O demo02 > demo2.sql
To Restore:
psql -U demo3 demo3 < demo2.sql
Related
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
I'm trying to move a postgres database between machines as I move from one development platform to another. I have yaml_db gem installed on both machines.
On my old platform I do:
rake db:scheme:dump
rake db:data:dump
When I go to reload the database on my new machine I've discovered that my 2 dozen foreign_keys are preventing me for loading my data. What are my options?
You're copying a database, Rails really shouldn't have anything to do with the process (and as you're seeing, it just gets in the way).
Instead, put on your DBA hat and copy the database without bothering with Rails. Dump the data using pg_dump and then restore the data with pg_restore. The database's backup/restore tools know all about foreign keys, triggers, extensions, and anything else that Railsy tools don't understand.
you can use pg_dump command to dump your database using:
eg:
pg_dump -U <user-name> -h <host> <database> > <file-name>.sql
pg_dump -U postgres -h 127.0.0.1 database1 > database1.sql
Then copy file to other machine and run following command to restore database
psql <database-name> < path/to/sql_dump_file
psql database1 < database1.sql
I am working with a Rails 4 app, I am using PSQL both in my development and production. Due to some reason I have to work with a new computer/laptop so I set up Rails environment in it and cloned my app into it, but what I really require is that, I need my existing databse with data in it, how to do it ?
To do this you need to create a dump on system 1 and restore it on system 2, here are the steps:
sudo -u postgres pg_dump <DB_NAME> > dump - creating a file dump
Copy this file via dropbox or whatever to another system.
sudo -u postgres psql <DB_NAME> < dump - copy the new created dump to new system.
Note:
You should have empty created database on your new system, or you can use dataonly dump passing the --data-only to pg_dump command.
Also you can read documentation for pg_dump to find any other options which you might need.
I just deployed my rails app to a Linode VPS, and was wondering what would be the best way of adding records to the DB.
I have tables such as Categories, which I'd like to populate.
I thought of the Taps gem, using a csv, or an sql dump file.
I'd like to know if there are any tools out there for this?
Thanks
For this puporse there are the so called seed file which is default in:
db/seeds.rb
You can add entries here ( there is an example in the seed file ), which you can generate after deployment with a rake task:
rake db:seed
You probably are using bundler as well, so use:
bundle exec rake db:seed
In case of large number of seeds you can always create multiple files, see this blogpost about handling large seed files.
However, if you are in a state, where the already existing data in the app is crucial and you are changing servers or database drivers you wanna take a look on yaml_db gem which gives a nice method to abstract the existing data away from your actual db driver and export it into a .yaml file which you can import later back e.g.: after deploying on a new server.
See Railscast - #179 about seeding.
The rails way would be to use seed data in db/seeds.rb and then populate it by using rake db:seed.
You could also use a sql dump file and restore by issueing mysql -u <user> -p <database_name> < <mysql_dump_file>
The Easy Reference Data gem is similar to db:seed, but will update records if entries already exist. It also has easy integration with Capistrano.
Full disclosure: The company I work for developed the gem.
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.