How to backup/restore standalone heroku postgres database? - heroku

Is there a Heroku way to backup / restore a standalone postgres database at https://postgres.heroku.com/ apart from using pg_dump / pg_restore ? The database is not attached to any app.
I can create a dump using
pg_dump --verbose -F c -b -h hostname -p port -U username -f "backup.dump" database_name
and restore it using
pg_restore --verbose --clean --no-acl --no-owner -h hostname -p port -U username -d database_name "backup.dump"

Looks when you create a new database, heroku automatically creates an app for you. Check it with heroku list So you can restore using the empty app.
heroku pgbackups:restore DATABASE 'dumped-db-secure-url' --app heroku-postgres-xxx

Related

pg_restore ignores .pgpass and PGPASSWORD environment variable

I want to import a backup using pg_restore without a password prompt.
I tried several options but after I run the script it will always ask for a password. pg_dump is working but not pg_restore. I can run the pg_restore command if I enter my password but I want a passwordless command or at least I don't want to enter my password because the script has to work without user interaction.
What is working for me:
PGPASSWORD=xyz pg_dump -h localhost -U user -Fc database > ~/dump_prod.pgsql
What is NOT working
1.)
PGPASSWORD=xyz pg_restore -h localhost -d database -U user -W --clean --no-owner ~/dump_prod.pgsql
2.)
pg_restore --dbname=postgresql://user:pass#localhost:5432/db -W --clean --no-owner ~/dump_prod.pgsql
3.)
touch ~/.pgpass
echo "*:*:*:*:password > ~/.pgpass
chmod 0600 ~/.pgpass
pg_restore -h localhost -d db -U user -W --clean --no-owner ~/dump_prod.pgsql
any ideas?
Regards
As per the doc, -W will prompt for a password. -w will not
-w
--no-password
Never issue a password prompt. If the server requires password authentication and a password is not available by other means such as
a .pgpass file, the connection attempt will fail. This option can be
useful in batch jobs and scripts where no user is present to enter a
password.
-W
--password
Force pg_restore to prompt for a password before connecting to a database.
The .pgpass file worked for me.
My setup:
I am restoring DB into the postgres instance running as Docker container.
The postgres instance is run using command:
docker run --name postgres_db -p 5432:5432 -e POSTGRES_PASSWORD=admin -d postgres:9.6
pg_restore is present but it is available as separate tool, outside docker setup.
The ~/.pgpass file entry looks like this:
localhost:5432:db_name:user:password
Where:
db_name is the target db name that one is going to restore.
user is the name of the user that is going to perform the restore - in my case an admin user. i.e. postgres user in the postgres instance.
password - admin user's password. i.e. admin

Postgresql data recovery encoding

I can't recover a database script.
I have followed different posts and this is what I've done so far to solve this:
1- dump UTF-8 old database to LATIN9:
"C:\Program Files (x86)\pgAdmin III\1.22\pg_dump" -E LATIN9 --no-owner --no-acl -U postgres -W -h localhost -p 5432 database_name > database_name.sql
2- Remove the old database
DROP DATABASE database_name;
3- Create the new database as LATIN9:
CREATE DATABASE "database_name" WITH ENCODING='LATIN9' OWNER=username TEMPLATE=template0 LC_COLLATE='C' LC_CTYPE='C' CONNECTION LIMIT=-1;
4- I try to recover the LATIN9 script created in the first step with the next command:
"C:\Program Files (x86)\pgAdmin III\1.22\psql.exe" -U username-d database_name -f database_name.sql
And then I get the error message:
psql: FATAL: conversion between WIN1252 and LATIN9 is not supported
I don't understand, the script is already LATIN9!!! What's going on?
I want to add that what I'm trying to archive with this is to change my current UTF-8 database to LATIN9.
To make it work I changed the steps 1 and 4.
This are all the steps with the corrections:
1-
"C:\Program Files (x86)\pgAdmin III\1.22\pg_dump" --host localhost --port 5432 --username "postgres" --role "postgres" --no-password --format tar --blobs --encoding LATIN9 --verbose --file "database_name.backup" "database_name"
2-
DROP DATABASE database_name;
3-
CREATE DATABASE "database_name" WITH ENCODING='LATIN9' OWNER=username TEMPLATE=template0 LC_COLLATE='C' LC_CTYPE='C' CONNECTION LIMIT=-1;
4-
"C:\Program Files (x86)\pgAdmin III\1.22\pg_restore" --host localhost --port 5432 --username "postgres" --dbname "database_name" --role "postgres" --no-password --verbose "database_name.backup"

Slimming down psql calls in database config creation

I currently have the following calls to psql to setup my PostgreSQL database. I'd like to slim this down as each time I have to input a password for every call. I haven't had much experience with psql so am not too sure about the best way to do all of the following in a shorter, more user friendly fashion.
Thanks.
# Create DB
psql -U postgres -h 127.0.0.1 -c "CREATE DATABASE \"main\" WITH OWNER maindb;"
# PostGIS setup
psql -U postgres --dbname main -h 127.0.0.1 -c "CREATE EXTENSION postgis;CREATE EXTENSION postgis_topology;CREATE EXTENSION fuzzystrmatch;CREATE EXTENSION postgis_tiger_geocoder;"
# Create users table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/Users.sql
# Create interests table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/Interests.sql
# Create user-interests table
psql -U maindb --dbname main -h 127.0.0.1 -a -f ../lib/sql/UserInterests.sql
each time I have to input a password for every call
Solve this part by using a .pgpass file. Then you can make as many calls to psql as you want.

How to easily DB dump to heroku's DB

I have a local db full of data that i want to push to the Heroku's DB in order to populae it.
What is the best way/tools o realize this ?
Thank you!
You could write a script that ports your current DB into a seed file, then run the seed file using heroku run rake db:seed
First dump your local database to a dump file:
PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
Replace myuser, mypassword, and mydb with your username, password and database name. If there is no password set, leave out the PGPASSWORD=mypassword part.
Next, you must place the mydb.dump file in a publicly accessible location, so upload it to an FTP server or Amazon S3 bucket (for example).
Then on your local machine run:
heroku pg:backups restore 'https://s3.amazonaws.com/me/mydb.dump' HEROKU_POSTGRESQL_COLOR_URL -a appname
Replace HEROKU_POSTGRESQL_COLOR_URL with the URL for your app's database. If you don't know the URL, you can find it with heroku config | grep HEROKU_POSTGRES. Replace https://s3.amazonaws.com/me/mydb.dump with the URL where you uploaded the dump file. Replace appname with name of your app as defined in Heroku.

Forking/Copying Heroku ClearDB to development environment

I'm setting up a development environment on heroku for my app and I'm having an issue copying over the DB. My current DB is ClearDB and I usually connect to it via Workbench. However, if I try to export the DB and iimport into my staging environment I get a credential issue.
I found this post on SO with regards to this issue:
Moving/copying one remote database to another remote database
And the solution is here:
mysqldump --single-transaction -u (old_database_username) -p -h (old_database_host) (database_name) | mysql -h (new_host) -u (new_user) -p -D (new_database)
But even if I run this, I'm still running into an issue with credentials. The execution wants both passwords at the same time, for old DB and new DB so it keeps failing.
I tried to inline the -p but it still asks for password. What am I missing?
Okay, that was a silly mistake. The reason I was having issues is that after option such as -u or -h, there is a space while in the option for password, there is no space. I.E.
mysqldump --single-transaction -u old_database_username -pPasswordOld -h old_database_host database_name | mysql -h new_host -u new_user -pPasswordNew -D new_database
Once corrected, everything was done.

Resources