Need help understanding Importing Heroku Postgres Databases with PG Backups - heroku

I'm reading the documentation here, and I have a few questions. Just to make sure I'm not misunderstanding what this terminal command is supposed to do.
Question 1:
I have an SQLite .db file on my local computer. With this command, I can convert this db into a mydb.dump file, which I can then import into Heroku Postgres. Is this intepretation correct? If so, we can move on to my second question.
Question 2:
Attempting to run PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump is causing me some issues. From what I can understand, the values I'm supposed to substitute are
mypassword (heroku postgres password)
myuser (I have no idea what's supposed to be here)
mydb (name of db)
mydb.dump (name of dump)
What exactly should go into each of these values, and where can I find them?

The pg_dump command dumps a PostgreSQL database. It has nothing to do with SQLite.
SQLite and PostgreSQL are very different databases. If you're going to use Postgres in production I strongly recommend using it in development as well. In general, your development environment (and any others, like a staging environment) should be as similar as possible to your production environment.

Related

Forgot to migrate free tier Postgres and now my app is empty

My project is again live no problems whatsoever, but the database is empty.
I am either trying to restore an old backup to the new Postgres instance or upload a backup from my local disk.
I tried to do pg:restore but I don’t know where to grab the backup (from the free Heroku tier).
I got the answer I was looking for by going into some of the most hidden help articles in heroku.
https://help.heroku.com/QG1W7LIJ/how-do-i-restore-a-partial-backup-or-single-table-to-heroku-postgres
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U YOUR_USERNAME-d CONNECTION_URL mydb.dump
Had to recreate the backup from the local imported database but at the end it worked flawlessly.

Mac: How to load Postgres Example Database using Postgress.app

I just downloaded the Postgres.app (9.4.5). I want to load (restore) the example database from http://postgresguide.com/setup/example.html. I can't seem to get it imported.
I opened a psql session via the app, exited back to the shell, created a .bash_profile in my home directory (per http://postgresapp.com/documentation/cli-tools.html), quit terminal and opened a new psql session and exited to the shell once again. I then ran the pg_restore command as shown on the example page. I saved the log, but it is 3K lines long and doesn't show any obvious errors. There is nothing in the pgadmin.log in my home directory.
When I start pgAdmin3, I can find no evidence that the database was loaded.
I'm brand new to Postgres, but experienced with Oracle and MySQL.
This is all I see in pgAdmin3. I created the one table in my schema manually.
Did I overlook something?
The pg_restore command on that page does NOT restore the database. It dumps the contents of the backup to stdout. It needs an additional switch '-d' to import directly into a database. But first you have to create an empty database. You can do that from pgAdmin lets say you call it exampledb then the restore would become:
pg_restore --verbose --clean --no-acl --no-owner -h localhost -d exampledb example.dump

Can I import .dump file to heroku through travis

I am having a QA instance which contains a very limited data(approx 30-35 MB) it contains images etc, thumbnails hence cannot put them into seed.
I have a private repo at github(synced with heroku) which contains a .dump file say abc.dump file and I want to run my test cases from travis, in the before script I will be restoring this dump.
I would like to know how to import this dump to heroku although this is available at heroku bash as well. I looked into the documentation here they say make it asseccible through http somewhere. I am trying using the pg_restore commands using the database username, passwords available at the application database overview available through UI, but not working.
Can someone suggest me a good way out to simply restore by not making my database public ?
Also. how often the database user/password is changed at heroku ?
As travis would be having access to abc.dump beforehand it can perform basic pg_restore function of postgresql.
PGPASSWORD=<PASSWORD> pg_restore --verbose --clean --no-acl --no-owner -h <PG_HOSTNAME> -U <PG_USER> -d <PG_DATABASE_NAME> -p <PG_PORT_NO> <DUMP_FILE>.dump
The above details for the heroku database hostname, user, pass, port etc. can be fount easily using :-
heroku pg:credentials DATABASE --app application_name

Push database to heroku: how to use heroku pg:push

I want to push my local postgresql database to heroku, using heroku pg:push command. The command looks like this: heroku pg:push mylocaldb DATABASE --app sushi according to the heroku document: https://devcenter.heroku.com/articles/heroku-postgresql.
Here is my local database info:
Name: mysitedb
User: bill
Password: bill
The DATABASE_URL environment variable in my machine is set to: postgres://bill:bill#localhost/mysitedb.
My app's name is secure-gorge-4090. I tried heroku pg:push mysitedb DATABASE --app secure-gorge-4090. The output was:
! Remote database is not empty.
! Please create a new database, or use `heroku pg:reset`
I was surprised that I have put nothing into my DATABASE. But I still ran heroku pg:reset DATABASE to reset my DATABASE. After that, I tried heroku pg:push mysitedb DATABASE --app secure-gorge-4090 again but the output was still the same.
I tried heroku pg:push postgres://bill:bill#localhost:8000/mysitedb DATABASE --app secure-gorge-4090. The output was:
! LOCAL_SOURCE_DATABASE is not a valid database name
I don't know how to use this command to move my local database to heroku. I need your help. Thanks!
Are you actually typing in the token DATABASE in your commands, or is that a placeholder you're using for this question? From the docs you linked to:
Like pull but in reverse, pg:push will push data from a local database into
a remote Heroku Postgres database. The command looks like this:
$ heroku pg:push mylocaldb HEROKU_POSTGRESQL_MAGENTA --app sushi
This command will take the local database “mylocaldb” and push it to the
database at DATABASE_URL on the app “sushi”. In order to prevent accidental
data overwrites and loss, the remote database must be empty. You will be
prompted to pg:reset an already a remote database that is not empty.
Usage of the PGUSER and PGPASSWORD for your local database is also supported
for pg:push, just like for the pg:pull commands.
When you do heroku config -a secure-gorge-4090, you should see an entry for HEROKU_POSTGRESQL_[SOME COLOR NAME]. Make sure you're using whatever that token is instead of DATABASE in your commands.
Since you have a username and password on your local database, you also need to do the part mentioned about PGUSER and PGPASSWORD. Here's the example from the pg:pull docs:
$ PGUSER=postgres PGPASSWORD=password heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi
So you should do something like:
$ PGUSER=bill PGPASSWORD=bill heroku pg:push mysitedb HEROKU_POSTGRESQL_[SOME COLOR] -a secure-gorge-4090
I know this is a old discussion but I had the exact same problem. Though it's not quite as convenient, I managed to achieve this via pg:backups instead.
This is detailed pretty nicely on the heroku support site
Start by installing the free pgbackups addon:
heroku addons:add pgbackups
Then backup the database using your local pg_dump utility (included in PostGreSQL distro)
pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dmp
Then put that dump file somewhere URL-addressible (e.g. Dropbox) and run the heroku import(make sure it's double quotes for Windows):
heroku pg:backups:restore 'https://dropbox.com/dYAKjgzSTNVp4jzE/mydb.dmp' DATABASE_URL
You need the following command
PGUSER=root PGPWD=root heroku pg:push (local database name) DATABASE_URL --app heroku (app name)
make sure that you have entered correct postgres username and password
I'm a lazy programmer and efficient so this is much more easier rather than paying for AWS backup store them in excel sheets.
This save cost and not use PUSH: PULL which is not efficient.
Using CMD as ADMIN to insert Excel data to Heroku Postgres Database.
Follow INSTRUCTION
1. OPEN CMD AS ADMIN
2. heroku pg:sql postgresql-rugged-08088 --app sample
3. CREATE TABLE SERIAL_T ( id SERIAL , SERIAL VARCHAR(50), USE INT, DEVICES TEXT[], PRINTED BOOLEAN, PRIMARY KEY (id))
4. \COPY SERIAL_T (SERIAL, USE, DEVICES, PRINTED) FROM 'C:\Users\PATH\EXCEL-03-27-2021.csv' DELIMITER ','CSV HEADER;
What worked for me without any issues was
pg_dump -f database_output_name --no-owner --no-acl -U user_name name_of_your_local_database
database_output_name name of the output file you can rename it whatever backup, database ...etc
user_name: the postgres sql user_name mainly postgres
name_of_your_local_database: is the name of your database: mydb or whatever the name you gave if you forgot it you can check in PgAdmin
pg_dump I have export it to global path that is why I am using it like this, other ways you can call it with the absolute path ie: C:\"Program Files"\PostgreSQL\14\bin\pg_dump
the grenerated ourput file shoule look something like this
--
-- PostgreSQL database dump
--
-- Dumped from database version 14.1
-- Dumped by pg_dump version 14.1
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: alembic_version; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.alembic_version (
version_num character varying(32) NOT NULL
);
...
Now run this command to create the database on heroku
heroku pg:psql --app heroku_app_name < database_output_name
If you want to reset your database you can run this command
heroku pg:reset -a heroku_app_name
Now you can check the database on heroku by clicking on postgres link
Please check this answer for more detail: pg_restore: error: unrecognized data block type (0) while searching archive while trying to import postgres database to heroku

Heroku mysqldump to remote host

I created a nice little Rake task to backup our Mysql database on ClearDB to a remote Google storage bucket. Works great locally but running it on Heroku I get this error:
sh: mysqldump: not found
Of course, Heroku doesn't have mysqldump, how silly of me, but is there any way to do this?
The command I'm using is something like this:
system "mysqldump -h host.cleardb.com -u user -p'password' --single-transaction database | gz > #{backup_directory}/#{file_name}"
Of course gzip doesn't exist either on Heroku.
I know there are a couple of gems to backup PostgreSQL databases but I haven't seen anything for Mysql.
This is for a Rails 3.2 app.
Any ideas would be appreciated.
I'd say your best option would be a custom build pack that adds the mysql binaries that you need.
https://devcenter.heroku.com/articles/buildpacks

Resources