Heroku Postgres Dump not creating Sequences - ruby-on-rails

Step1 Capture backup from Heroku
Step2 Download latest.dump from Heroku with Postgres 10.2 version
Step3 Exporting Heroku Dump in my Local Postgres 9.6.
Result: its import all data but not creating sequences in by Database which producing error on each creation query.

You can export a sequence using the following command
pg_dump -t your_sequence_name
If you're not sure how your sequences are named, try this command using psql
\d your_table_name
You might also want to take a look at pg_dumpall
pg_dumpall > your_db.out

Related

Duplicating Ruby on Rails 5 project with PostgreSQL database

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

How to deal with foreign keys when moving a postgres database between machines

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

Rails 4 App : How to set up the same application along with PSQL Database in New System?

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.

Pushing a single table to Heroku

I am aware of the heroku pg:push command which pushes an entire database up to Heroku.
Now that I am launching my product, I would like to be able to push up only a specific table that contains information collected locally without overwriting existing tables (such as users).
Is there a command that enables me to only push specific tables to heroku?
My suggestion is to use PostgreSQL dump/restore capabilities directly using the pg_dump and psql commands.
With pg_dump you can dump a specific table from your local database
$ pg_dump --data-only --table=products sourcedb > products.sql
Then grab the Heroku PostgreSQL connection string from the configs
$ heroku config | grep HEROKU_POSTGRESQL
# example
# postgres://user3123:passkja83kd8#ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398
and restore the table in the remote database, using the information retrieved from Heroku.
$ psql -h ec2-117-21-174-214.compute-1.amazonaws.com -p 6212 -U user3123 db982398 < products.sql
You will need to customize the -p, -h and -U parameters, as well as the database name. The password will be prompted by psql.
You can also use the pg_restore to filter a dump and restore the table, but I personally prefer psql.
Note that Heroku is recommending the use of PostgreSQL tools in several documentations, such as Importing and Exporting for large data, or whenever the provided CLI commands don't cover specific cases like the one in this question.
I wrote script which extracts DB url from heroku. Then it dumps single tables from production and restores them on development/localhost. Run it like this:
rake production_to_development:run\['users;news;third_table',my-sushi-app\]
Code:
namespace :production_to_development do
task :run, [:tables, :app] => [:environment] do |t, args|
tables = args["tables"].split(';')
database_url = nil
Bundler.with_clean_env { database_url = `heroku config:get DATABASE_URL --app=#{args["app"]}` }
require 'addressable/uri'
uri = Addressable::URI.parse(database_url)
remote_database = uri.path[1,uri.path.length-2] # there is \n at the end of the path!
tables.each do |table|
backup_file = "tmp/#{table}.backup"
#bin_dir = "/Applications/Postgres.app/Contents/Versions/latest/bin"
bin_dir = ""
dump_command = "PGPASSWORD=#{uri.password} #{bin_dir}/pg_dump --file \"#{backup_file}\" --host \"#{uri.host}\" --port \"#{uri.port}\" --username \"#{uri.user}\" --no-password --verbose --format=c --blobs --table \"public.#{table}\" \"#{remote_database}\""
`#{dump_command}`
`psql -U 'root' -d my_table -c 'drop table if exists #{table}'`
`pg_restore -d my_table --no-owner #{backup_file}`
end
end
end
If I understand correctly, you just need a single database table with its locally created data pushed to your Rails production app. Maybe this is a simplistic approach, but you could create a migration for your table and then populate using db/seeds.rb.
After you've populated the seeds.rb file and pushed your repo to heroku:
heroku run rake db:migrate
heroku run rake db:seed
Also, if your local table has a ton of data and you're using Rails 4, check out the seed dump gem: https://github.com/rroblak/seed_dump. This will take your existing db data and map it to the seed format.

Transfer initial PostgreSQL database from development to Heroku production

I have an initial set of production data stored locally in the development database that I'd like to migrate to production for a starting point for data. What's the best way to transfer this data?
It does not seem evident if there is a way to use pgbackups as per the instructions. Perhaps I have to run a manual backup of some sort locally and then push it over with pgbackups and if that is the case, I'd appreciate some specific instructions on accomplishing this.
After some additional digging and an answer from Heroku, the answer for importation of initial data is to:
1) If using PGSQL locally, first dump the data:
pg_dump -U your_username your_database_name -f backup.sql
2) Then follow the instructions found here for importation to Heroku's database:
http://devcenter.heroku.com/articles/pgbackups#importing_from_a_backup
First dump your local database using pg_dump:
pg_dump -Fc --no-acl --no-owner -h ... mydb > mydb.dump
and then use heroku pgbackups:restore:
heroku pgbackups:restore heroku-database-url url-to-your-mydb.dump-file
Note that the mydb.dump file needs to be accessible by the Heroku servers.
The Heroku Dev Center page has detailed instructions:
https://devcenter.heroku.com/articles/heroku-postgres-import-export

Resources