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.
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 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
First of all, this may look like a duplicate of:
postgres hstore exists and doesn't exist at same time
but it is not. While I am getting the same error message in the circumstance. When checking to see if hstore is installed on the DB, we can see that it is:
./psql -d photographerio_development -c '\dx'
List of installed extensions
Name | Version | Schema | Description
---------+---------+------------+--------------------------------------------------
hstore | 1.2 | hstore | data type for storing sets of (key, value) pairs
plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language
and it is too on the template_1 DB.
So, when I try to run the migration to add the hstore, I get the PG::Error: ERROR: extension "hstore" already exists and when I comment out this migration, on the next one, which requires the hstore, it says PG::UndefinedObject: ERROR: type "hstore" does not exist which is a bit of a paradox.
It is a Rails 4.0.1 app with postgresql 9 and I have hstore working on a few other projects running on this machine.
You have installed the hstore extension in a schema named hstore which is presumably not on your default search_path.
You must do one of these:
Add hstore to search_path during connection setup;
Add hstore to search_path with an ALTER USER ... SET or ALTER DATABASE ... SET;
Move the hstore extension from the hstore schema into public; or
schema-qualify all references to hstore, e.g. hstore.hstore(...). This must be done for operators too; -> becomes OPERATOR(hstore.->)
This is how I solved the problem.
Create a schema called extensions and grant authorization to the db username you use in the project.
psql -U postgres -d template1 -c "CREATE SCHEMA extensions AUTHORIZATION <yourDbUserName>;"
create the extensions in the created schema (extensions)
psql -U postgres -d template1 -c "CREATE EXTENSION IF NOT EXISTS hstore SCHEMA extensions;"
This is how I solved this issue in ubuntu 18.04.
Provide postgres super user access.
sudo su postgres
Then I run:
psql -U postgres your_database_name -c 'create extension hstore;'
Now I can alter table your_database_name and add hstore type columns in it.
Connect to your database
psql -d your_database_name -U your_user_role
And
alter table your_table_name add your_column_name HSTORE;
Though there might be saveral different ways to do it, but I solve it in this way.
Hope this will help novice users like me.
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