i tried to create multiple rails app . I just deleted the file doing rm -rf . Now when im trying to create a new rails app with the same and do db:create , it tells me that ex: "blog_development" already exist . How can i delete the database that i created that i just deleted the directory but didn't drop the database ? .
I tried this in other rails app directory ActiveRecord::Migration.drop_table(:table_name) but it says the blog_development does not exist
You can do it in your DB console. For example, if you're using mysql:
Login to Mysql: mysql -u <username> -p
Delete the DB: DROP DATABASE <db_name>
Or you can just skip creating the database in your new app and just go straight to rake db:migrate
Related
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 have db named "mydb". Now How can I create a new db with name "mydb_test" with only schema of "mydb"
Tried the following link PostgreSQL how to create a copy of a database or schema?
As mentioned over there I tried the following command,
createdb -T olddb newdb
This copies including the data into newdb
If I try the second option mentioned in the above link I get following error,
# pg_dump -Cs -U postgres my_test_db > dump_schema_file
# psql -U postgres naggappan_my_test_db < dump_schema_file
psql: FATAL: database "naggappan_my_test_db" does not exist
How can I take only schema
You may create a new dump without data of required schema and then restore from it into a new DB:
# pg_dump --dbname=source_db_name --username=postgres --encoding=UTF8 --schema=db_schema --schema-only --file=path_to_filename.dump
# psql --dbname=target_db_name --username=postgres --file=path_to_filename.dump
If you have an existing dump with schema and/or data in binary format, you can restore only the schema from it using pg_restore:
# pg_restore --dbname=target_db_name --username=postgres --schema-only path_to_filename.dump
When you generate a new rails app with postgres as db it automatically set username to appname in database.yml.
You then have to manually change it to your username or the app will not run.
Is there any way to automatically set it to logged in user (at time of running rails new) or other preset username?
In development and test environments it will always be the same username and production does not matter as it always deployed on heroku.
I am on ubuntu, but would be great to hear solutions for mac users also.
Ok I ended up doing this:
In my template I added this line to replace the username:
gsub_file "config/database.yml", /username: .*/, "username: myusername"
I have a simple workflow of sole developer on dev machine so usernames for developement and test will always be the same and production does not matter as it deploys to heroku.
It is a good habit to make a new user for each application. to do this, first create adminpack extension as follows:
$ psql postgres -c 'CREATE EXTENSION "adminpack”;'
Open database prompt to create a new user. lets say your app called blog:
$ psql -d postgres
postgres=# create role blog login createdb ;
postgres=# \q
Now create your database and associate that to the user you just made:
$ createdb -E UTF8 -w -O blog blog_development
To check of its created:
$ psql -l
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
Can I add the migration file, but tell rails not to run it locally? I need the file for others to setup the application locally.
If you have a file:
db/migrate/20121010100909_modify_table_x.rb
You can go into your database and run the following SQL.
MySQL:
INSERT INTO 'schema_migrations' VALUES ('20121010100909');
PostgreSQL:
INSERT INTO schema_migrations VALUES ('20121010100909');
And it will then ignore that migration.
Edit - How to "go into your database"
Using the parameters from config/database.yml in Rails, connect to the database you are using.
You will need to use the command-line tool of whatever database software you're using. E.g.
For PostgreSQL:
psql -d <database_name> -U <username>
For MySQL:
mysql -u <username> <databasename>#localhost -p
Type in your password if required.
Then type in and execute the SQL above.
You could insert the proper timestamp into the schema_migrations table locally.