heroku pg:push remote database is not empty? - heroku

I am trying to pg:push my database to heroku server but
still getting the remote database is not empty error although
I am resetting/ the db with pg:reset
heroku pg:reset remote_database_name
heroku pg:push dev_database remote_database
Still getting the error.
For the remote database name I am using the name my-database-7121::pink

I had this problem and it took me a while to solve.
The trick is making sure Postgres is set up properly. To check that it is, type the following into your Terminal:
$ which psql
If you're on a Mac and you're using Postgres.app, this should return something the directory where Postgres.app can be found, e.g.:
$ /Applications/Postgres.app/Contents/Versions/9.3/bin/psql
If the 'which psql' command returns nothing, restart the Terminal and type in the following:
$ export PATH=$PATH:/Applications/Postgres.app/Contents/Versions/9.3/bin
Check that this command now works (i.e. returns a directory):
$ which psql
If it does, you should be able to carry on with the 'heroku pg:reset' and 'heroku pg:push' - only this time it will (hopefully) work

Related

Trying to pull Heroku database onto my localhost

I recently made a mistake by rolling back my database too far on my localhost and removed all the data from within the databases I've had. I did migrate the databases so they would return but they still were cleaned out.
Asking around what to do next, I was told that I should backup the database on Heroku (which has all the current information) and pull it onto my localhost. If this is the correct action to take, then I am still facing a problem. When I type:
heroku pg:pull DATABASE_URL mylocaldb --app (my_app_name) it returns an error of:
sh: createdb: command not found
!
! Unable to create new local database. Ensure your local Postgres is
working and try again.
I do not have the best understanding of databases as I am newer; however, I have downloaded sql on my local machine. All of the commands I input are in the regular console and not the psql terminal.
If anyone has any knowledge or can help me, I would greatly appreciate it. Thank you so much!
Joe
Edit: If you do have another method/solution, please let me know. I cannot update my application until I have this done so I do not lose my information.
I would try using backup/restore:
heroku pg:backups --app MYAPPNAME capture
curl -o latest.dump `heroku pg:backups --app MYAPPNAME public-url`
as (almost) documented at
https://devcenter.heroku.com/articles/heroku-postgres-import-export
Note the strange placement of --app MYAPPNAME. It needs to go immediately after the heroku sub-command and before any other arguments.

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 rake db:structure:load failure

I need to use some PostgreSQL proprietary features such as rules and triggers for table partitioning. As long as I know, these kind of features cannot be dump to schema.rb so I have changed my schema_format configuration parameter to :sql.
Now, when I try to load rake db:structure:load to load the generated structure.sql into the heroku database, it fails saying: sh: psql: not found
How can I do it?
You can use pg:psql to run the script from your development machine against the database:
cd your-rails-project
heroku pg:psql -a your-app-name <db/structure.sql
Just make sure that the branch you have checked out locally is the same as the one you have deployed.

Heroku, data import error with mysql2

I am beginner on Heroku.
I push my exist ruby on rails application into heroku and that was fine.
Next i push my mysql data into heroku with 'push' command as following.
$heroku config:add DATABASE_URL='mysql2://<my CLEARDB_DATABASE_URL>#<myapp>.herokuapp.com/heroku_db?reconnect=true'
$heroku db:push mysql2://<my CLEARDB_DATABASE_URL>#<myapp>.herokuapp.com/<my dump file>
But i got error as following.
Failed to connect to database:
Sequel::DatabaseConnectionError -> Mysql2::Error: Can't connect to MySQL server on '<myapp>.herokuapp.com' (110)
I am confusing what should i do.
Someone tell me how to resolve it.
Many thanks.
-Ono
Don't use db:push or db:pull. Please export a sql file locally and either pipe it in like so:
$ heroku pg:psql DATABASE_URL -a app_name < file.sql
Or better, use Postgres locally and use pgbackups to import/export like so: Importing and Exporting Heroku Postgres Databases with PG Backups

How can I pull an existing heroku app to new location for development?

I currently have the latest version of my code on another computer that I want to develop from (Home computer and laptop for when I'm out and about) I set up heroku for my app on my laptop. Now I need to associate my code on my desktop so that I can push to heroku from there as well.
This is what I get from my desktop:
desktop:~/NetBeansProjects/onlinescheduler$ git pull heroku master
fatal: 'heroku' does not appear to be a git repository
fatal: The remote end hung up unexpectedly
I can't do heroku create because that will create a separate app. How do I associated the existing code with (or pull down a brand new version from) heroku?
Whats the command to do this?
Also, If you've never used heroku before on the other machine, you'll need to do a few more things first:
$ gem install heroku
$ heroku login
[then enter your credentials]
$ heroku keys:add [path to keyfile]
Now you can clone the remote repository:
$ git clone git#heroku.com:<heroku_app>.git <local_directory>
First of all, you'll want to follow the Quick Start instructions for Heroku, which you can get straight from the horse's mouth, right here: https://devcenter.heroku.com/articles/quickstart
Once you've gotten through step 3, come back here.
Then, you can type this into the command line:
heroku git:clone -a myapp
This is described here:
https://devcenter.heroku.com/articles/git-clone-heroku-app
Then, if you want to grab the database too, here are some options.
Newer Heroku instructions on import/export:
https://devcenter.heroku.com/articles/heroku-postgres-import-export
Older heroku instructions on push and pull: https://blog.heroku.com/archives/2009/3/18/push_and_pull_databases_to_and_from_heroku
If you are using mongo, this is a useful tool to sync your mongo database: https://github.com/pedro/heroku-mongo-sync#readme
If you first need to get the app from Heroku, clone your app.
To do that, write in your Terminal:
heroku git:clone -a your_app_name
If you already have the app and the remote to heroku follow the next steps. If not, you can check instructions here https://devcenter.heroku.com/articles/git
Find the name of your database
Write in your Terminal:
heroku pg:info -a your_app_name
it will look something like this:
HEROKU_POSTGRESQL_MAROON_URL
Find the name of your local database
In your Rails app go to config/database.yml
it will look something like this:
your_app_name_development
Clone your production database (PostgreSQL)
Write in your Terminal with your own database names:
heroku pg:pull HEROKU_POSTGRESQL_MAROON_URL your_app_name_development -a your_app_name
HEROKU_POSTGRESQL_MAROON_URL is an example of how could be the name of your production database (in Heroku):
my_app_name_development is the name of your development database (locally)
the_name_of_my_app is the name of your app in Heroku
Don't forget to finish this with bundle install...
If you already have your code base ready and have heroku setup, use:
$ heroku git:remote -a your_heroku_app
This will allow you to deploy from your new location.
Reference: https://devcenter.heroku.com/articles/git#creating-a-heroku-remote
Once you create a key in a new computer, you have to upload your new SSH key by typing heroku keys:add.

Resources