Rails - importing Postgres production database to local development database - ruby-on-rails

I have a database dump named dump.sql from production DB and when I try to import it to my local development DB I get an error
ERROR: role "petdoctors_prod" does not exist
\connect: FATAL: database "petdoctors_prod" does not exist
It looks like the name of the production database is petdoctors_prod and my local development db is called petdoctors_development. Is it possible to import prod DB to dev?
What's the best way of dumping the db from prod?

The following steps should help in importing the dump.sql to your local machine:
Run rake db:drop from on your local machine to drop the existing database
Run rake db:create. This would create the database mentioned in your database.yml file as per the environment. In this case, it would create a database called petdoctors_development
Import the dump to petdoctors_development. This syntax of this step would vary depending on the database you're using. For example, to import a dump in PostgreSQL, we use psql <db_name> < <Path to dump file>
Run rake db:migrate, in order to run all the migrations in your app
Post this, the app will run on development, with the same data as that of the dump taken from production.

Related

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!

Rails import database on heroku

I am trying to import a database from my computer to heroku but it keep on saying unknown command for that.Even I am confuse about it which command to use .I have a postgre sql database.I want to know how to import a database
push from SOURCE_DATABASE to REMOTE_TARGET_DATABASE
REMOTE_TARGET_DATABASE must be empty.
SOURCE_DATABASE must be either the name of a database
existing on your localhost or the fully qualified URL of
a remote databas
So what will be the remote_target_database ???
and do i need to specify my full path to source database ?
Try the following.
heroku pg:push your_app_name_development DATABASE_URL
PG Backups can be used as a convenient tool to import database dumps from other sources into your Heroku Postgres database.
https://devcenter.heroku.com/articles/heroku-postgres-import-export
How to import a Heroku PG dump into local machine

How to load Postgres from schema and restore the data from existing from table?

I'm working on a rails application where we made changes to the schema directly without writing migration files. Now I want load the schema in the development machine to Staging server. But if I did rake db:schema:load, It will erase the data in the database.
Is there anyway to solve this problem? I want to restore the data in the table after running rake db:schema:load.

SQLite > PostgreSQL Rails migrations

I'm developing on my localhost using sqlite and deploying to Postgres on heroku, where the live app is already on heroku.
I've done a lot of development lately which includes several db structure migrations, so I've pulled the Postgres live database to my localhost to run the migrations locally to test (and swapped my local db to Postgres).
When I run rake db:migrate, it tries to run all the migrations from the beginning of time, rather than just the new ones.
How does rake know which migrations to run?
When you migrate first time , then rake will run all the migration .
When you run db:migrate, rails will check a special table in the database which contains the timestamp of the last migration applied to the database. It will then apply all of the migrations with timestamps after that date and update the database table with the timestamp of the last migration. As a result, each migration class is applied exactly once to the database.
source: https://stackoverflow.com/a/6319952/1970061
previously your database was different i.e sqlite, but when you switched to postgres you created new database, hence all table must be created for new database therefore all migration is running

Check db status through DATABASE_URL

I've been given access to Heroku application with rather strange setup. It has one database but when I run heroku config, I get different DATABASE_URL and HEROKU_POSTGRESQL_BRONZE_URL.
When I run heroku pg:info I get the following result:
=== HEROKU_POSTGRESQL_BRONZE_URL
Plan: Dev
Status: available
Connections: 1
PG Version: 9.2.4
Created: 2013-09-05 11:02 UTC
Data Size: 6.5 MB
Tables: 0
Rows: 0/10000 (In compliance)
Fork/Follow: Unsupported
I realised that my database is at DATABASE_URL, but I can't access that database, only through heroku run console. All heroku pg commands fail with this message:
! Unknown database: DATABASE_URL. Valid options are: HEROKU_POSTGRESQL_BRONZE_URL
If I run heroku pg HEROKU_POSTGRESQL_BRONZE_URL, I get access to empty database from above.
Since I have some issues with running migrations, I think my database might be full, and I'd like to check it. Any ideas how I can do that?
Here's the error after I run heroku run rake db:migrate:
PG::Error: ERROR: permission denied for relation schema_migrations
: INSERT INTO "schema_migrations" ("version") VALUES ('20130918114202')
More information about the setup:
rails 3.2.12
RAILS_ENV: staging (I don't have access to production, but I know this is "dev" server and there's also real "staging" from which this app was forked).
I have same problem and i fixed it:
Just Keep a Backup from your database and restore it back again, here is the steps:
heroku pg:info <-- to get the Database Name
heroku addons:add pgbackups <-- make sure you have the addons for backup
heroku pgbackups:capture <-- Capture the backup
heroku pgbackups <-- check your backups and make sure its there
heroku pg:reset DATABASE_NAME <-- Reset your database don't worry we have a backup, replace DATABASE_NAME with database name
heroku pgbackups:restore DATABASE_NAME b001 <-- Restore the backup again, replace DATABASE_NAME with database name and b001 with your Database version you can see this version number in heroku pgbackups step
heroku run rake db:migrate <-- Now you can run your migration and Operate in normal mode.
This seems like something screwy on the Heroku side. Have you tried submitting a ticket with them? I've always had good luck with their support.
I just got an update from my client. Before, I couldn't drop the database, because of the data in it. At the end, we decided to drop the database since the data can be added back (it's dev server, doesn't matter if we loose some dummy data).
I didn't find a solution for the problem above, but promoting HEROKU_POSTGRESQL_BRONZE_URL and restoring from backup solved the issue about not being able to access the database.

Resources