Heroku pg:pull not possible due to failed authentication - postgresql

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?

Related

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

With Gitpod, Heroku Postgres returning Error: FATAL: role ... does not exist

I have a Heroku app and it has a Heroku postgres addon attached. Im trying to connect to the remote heroku database from the Gitpod (online IDE) command line.
heroku pg:psql -a my-heroku-app
But its giving the following error:
--> Connecting to postgresql-rigid-XXXXX
psql: error: could not connect to server: FATAL: role "xxxxxxxxxxxxxxx" does not exist
And I get the same error when I try to connect with the connection URL
psql postgres://xxxxxxxxxxxxxxx:ppppppppppppppppp#ec2-0000000000.compute-1.amazonaws.com:5432/ddddddddddddd
I have properly installed postgres and heroku-cli in Gitpod and they work fine.
Appreciate your help.
Thanks!
Ok I found the answer thanks to this and this.
heroku pg:psql does'nt seem to work in Gitpod. So I followed this to import a db dump file into heroku postgres. From Gitpot command line I did
pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
and got the db dump file from my local (Gitpod) database. Uploaded it to my google drive and got the publicly downloadable link. Then did
heroku pg:backups:restore https://drive.google.com/u/0/uc?xxxxxxxx DATABASE_URL -a my-heroku-app
Now I have all the data in my heroku postgres database.
Cheers!

Heroku pg:push psql: FATAL: password authentication failed for user

I know similar questions have been asked but none of the solutions have worked. I am trying to push my local db to my Heroku db, and I keep getting psql: FATAL: password authentication failed for user "windows username".
I am on windows, so I tried SET PGUSER=postgres SET PGPASSWORD=password
Then ran heroku pg:push localdb DATABASE_URL --app herokuapp
But am still getting this stupid password error. The thing is it still looks like it is using my windows user name and not postgres username.... how do I resolve this?
Thanks to Heroku support I was finally able to get this to work. So for Windows users, these are the steps:
First you want to dump your local database out to a dump file:
pg_dump --verbose -F c -Z 0 -U postgres -h localhost -p 5432 yourdbname > local.dump
Then you want to grab the connection string from your heroku application config vars:
heroku config:get DATABASE_URL
Then you want to pick out the username / hostname / databasename parts from the connection string, ie: postgres:// username : password # hostname : port / databasename
One warning, running this against a production database with real data is something you want to avoid so be careful with pg_restore. When running this manually you run the risk of mangling your data without the CLI check, so you may want to manually verify that the target database is empty first.
pg_restore --verbose --no-acl --no-owner -U username -h hostname -p 5432 -d databasename < local.dump
Then when prompted for a password, just paste in the password from the connection string
Run SET PGUSER=postgres. This is important as otherwise heroku will use a different user and your password will not work.
Run heroku pg:push localdb DATABASE_URL --app herokuapp
Enter your password for postgres when it prompts.
I just faced the exact same problem and was successful in resolving this.
Rather than use the single line 'heroku pg:push' command with / without username/password, I relied instead on 2-steps:
Step-1: pg_dump
Step-2: pg_restore
This as pointed out by https://stackoverflow.com/users/4051445/na-peters above
as well as briefly hinted by Heroku at:
https://devcenter.heroku.com/articles/heroku-postgresql#pg-psql
Key thing is the password to enter is NOT the password you'd use when accessing your local Postgresql database. Instead, the password is a 64-character string you will obtain from:
heroku config:get DATABASE_URL -a
To extract it follow instructions by NA Peters above (it is the y string between : and # in the postgres://xxxxxxx:yyyyyyyyyyyyyyyyyyyyyyyy#hhhhhh:5432/dddddd
Expect it to work
For you Windows PowerShell folks out there struggling with the restore, try:
Get-Content -raw local.dump | pg_restore -F c --verbose --no-acl --no-owner -U <username> -h <hostname> -p 5432 -d <databasename>
Then paste the password in when prompted.

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).