Charles Duporge
Charles Duporge

Reputation: 672

Import SQL file into Rails database

I want to import a .sql file so that my new schema and datas is the one and the .sql file (so I don't care about losing my current schema and datas)

I tried rails db < sql-file-path but it returns me an error :

enter image description here

I've found this answer but it's not clear what this is really doing, like we have to create a migration to execute ruby code and import a file ? Every solution is welcome :)

Upvotes: 0

Views: 3062

Answers (2)

port5432
port5432

Reputation: 6381

In this case I would follow these steps:

  1. Backup your database
  2. Run the script
  3. Create Rails models for any new tables you imported
  4. Recreate the schema.rb file from the database

    bundle exec rake db:schema:dump

Upvotes: 0

Ronan Lopes
Ronan Lopes

Reputation: 3398

You don't need do necessarily with rails. You can use only mysql if you're comfortable with it. You can access mysql console by:

mysql -u user_name -p 

Exchange "user_name" by your user (root for example), then it will ask for your user password. When you have access to the console, you can select your development database by:

use your_database_name;

and then, to import:

source your_sql_file.sql

You can check your credentials and your database name on your database.yml.

If it doesn't work, please check your sql syntax or try to generate the dump file again.

Good luck, hope this helps!

Upvotes: 2

Related Questions