Where Heroku stored the latest.dump file, when I import db from Heroku to local Postgres? - postgresql

This is how I import db from Heroku Postgres to local Postgres db:
Back up your database from Heroku
heroku pg:backups:capture -a your-app-name
Download the database from Heroku. The database will be downloaded as a latest.dump file to your computer
heroku pg:backups:download -a your-app-name
Import latest.dump to Postgres (your local database) (if this is the first dump, you don't need to specify the number of dump)
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U your_local_db_username -d your_local_db_name latest.dump.[number of dump]
I want to know where Heroku stored the latest.dump file from step number 2 in my computer. In which folder? Heroku silent about it...

By default, it just puts it wherever you are on the filesystem when you run the command. If you didn't use the -a / --app argument, that's probably your project directory since Heroku would have to infer your app from your Git remotes.
You can use the -o / --output argument to put it elsewhere or give it a different name:
-o, --output=output location to download to. Defaults to latest.dump

Related

Heroku pg:pull not possible due to failed authentication

after hours of searching through Google I couldn't find a working solution to my problem.
I am using Heroku with Postgres the very first time and want to download a database backup and store it locally. I followed the instructions from here https://devcenter.heroku.com/articles/heroku-postgresql#local-setup but I fail when importing the backup into my local db.
I did heroku pg:backups:download --app appname and received the file on my desktop. Now when doing pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump I have to enter a password but I don't know which password I have to enter here. I already tried to set the username + password as variable but I am always ending up with a failed authentification. I also tried heroku pg:pull DATABASE_URL localdb --app appname but the story is the same as above.
Can anyone please clarify how I can do this?

Migrate PostgreSQL database from Heroku to Railway

I am trying to find out how to migrate my PostgreSQL database easily from Heroku to Railway. I have tried using heroku pg:backups:capture --app APP_NAME and heroku pg:backups:download with pg_restore and no success.
I had the error
pg_restore: error: corrupt tar header found in PGDMP (expected 0, computed 19471) file position 512
TL;DR:
heroku login
heroku run 'pg_dump $DATABASE_URL' > <filename.sql> --app <heroku-app-name>
PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -p $PGPORT -d $PGDATABASE -f <filename.sql>
Details:
If you logged in successfully after running heroku login, $DATABASE_URL is read from your Heroku environment, so no need to insert the database url by hand.
<filename.sql>: specify any filename you like. You will use it later to import the database.
<heroku-app-name>: The name of your Heroku (backend) app with the Postgres database.
Provision a new PostgreSQL database in your railway project and grab $PGPASSWORD, $PGHOST, $PGUSER, $PGPORT, and $PGDATABASE from the Variables tab.
This is what it should look like
Example (with dummy credentials):
heroku login
heroku run 'pg_dump $DATABASE_URL' > mydatabasebackup.sql --app my-heroku-backend
PGPASSWORD=hjUasj8hasA6ahsjJash -h containers-us-west-15.railway.app -U postgres -p 6473 -W -F t -d railway mydatabasebackup.sql

Ruby On Rails How to Copy local Postgres db to Heroku

I tried this on RubyMine Terminal:
heroku pg:push ror_development postgres://budobqbtsbzmlx:hUNYKkaMap-cExovtPJat4ajPm#ec2-54-217-208-1 58.eu-west-1.compute.amazonaws.com:5432/dbhsnsvf36h8tu
But its return me error:
Unknown database: postgres://budobqbtsbzmlx:hUNYKkaMap-cExovtPJat4ajPm#ec2-54-217-208-158.eu-west-1.compute.amazonaws.
com:5432/dbhsnsvf36h8tu. Valid options are: DATABASE_URL, HEROKU_POSTGRESQL_ONYX_URL
and also this error:
Connecting to HEROKU_POSTGRESQL_ONYX_URL (DATABASE_URL)
'psql' is not recognized as an internal or external command,
operable program or batch file.
Can somebody show detail sample of doing this action on windows ?
The error message states that you should use the environment variables rather than explicit names. The documentation states you should do something like:
heroku pg:push mylocaldb HEROKU_POSTGRESQL_ONYX
That said, I've never used this technique but did manage to successfully push my local db to heroku postgresql using this procedure.
Here's the short version:
install pgbackups:
heroku addons:add pgbackups
backup your local db:
pg_dump -Fc --no-acl --no-owner -h localhost -U <user> <dbname> > local_pg.dump
upload this file to a web server (I use S3 or dropbox). Let's assume http://www.dropbox.com/me/local_pg.dump
now restore into your heroky db:
heroku pgbackups:restore DATABASE 'http://www.dropbox.com/me/local_pg.dump'

heroku pgbackups:restore: an error occurred and your restore did not finish

I am attempting to restore a local snapshot of a database into a heroku dev instance.
heroku pgbackups:restore --app app_name HEROKU_POSTGRESQL_AMBER_URL https://www.dropbox.com/etc
But I consistently receive the following error
HEROKU_POSTGRESQL_AMBER_URL <---restore--- db.dump
Retrieving... done
! An error occurred and your restore did not finish.
The database snapshot was captured using the pg_dump string in the heroku docs:
PGPASSWORD=pwd pg_dump -Fc --no-acl --no-owner -h localhost -U postgres db > db.dump
and the dump file is in a dropbox share (so available with direct URL access)
Both Heroku and the local database are PG 9.2.
Heroku logs contain all of the output from pgbackups, which will help track down the issue.

Importing a postgresql dump to Heroku

I have a java app and postgresql database to go with it that is running on Heroku. I can push my app just fine, but what about the DB contents? I have exported a full dump from the database, but I don't know how I could import that.
By googling, you can find about db:push which is a limited rubygem, not pushing all the stuff needed. I have sequences, bigint datatypes etc. I also tried importing using heroku pg:psql --app MYAPP < db_all.out which just connects and stops, and going to heroku pg:psql --app MYAPP and issuing \i db_all.out complaints about permissions.
How should I do it?
You can run the pg_restore command from your local machine using the credentials given by heroku pg:credentials HEROKU_POSTGRESQL_<COLOR>.
To help others who still stumble upon this issue, what works for me is hgmnz's answer, but with a few modifications.
To be more precise:
Create a dump from the source PostgreSQL database
$ PGPASSWORD=YOUR_PG_PASSWORD pg_dump -Fc --no-acl --no-owner -h localhost -U YOUR_PG_USER YOUR_DB_NAME > YOUR_DB_NAME.dump
Get the Heroku Postgres credentials for your heroku app
$ heroku pg:credentials:url -a YOUR_APP_NAME
Attempt to import PostgreSQL dump to Heroku using the credentials above
$ pg_restore --verbose --clean --no-acl --no-owner -h HOSTNAME -U USER -d DATABASE -p PORT PATH/TO/YOUR_DB_NAME.dump --password
Enter the password received from the Heroku Postgres credentials
It should then import the dump successfully
This is very simple and had worked for me:
heroku pg:psql -a {YOUR_APP} -f {YOUR_DUMP_PATH}
I find this a better way then using the standard input syntax (like in the OP example), since it uses an option given by the Heroku command itself.
You may want to check if your dump file it's OK before submitting (in my case it wasn't).