Putting local Postregsql database in Heroku - django

I am creating a Django app on Heroku. While I am testing it locally, finally I am checking by uploading everything to heroku and seeing if it works for real on the remote server as well
To quickstart, I went to the admin panel and created a bunch of data to run the view functions on. When I am pushing the source code to heroku via git (git push heroku master), I also want to push up the database from local to heroku, so that I dont need to enter it again on the server side
How do I achieve this?
Thanks a lot

Grab your the postgres string from your Heroku database using heroku config:get
Look for the Heroku Postgres url (example: HEROKU_POSTGRESQL_RED_URL: postgres://user3123:passkja83kd8#ec2-117-21-174-214.compute-1.amazonaws.com:6212/db982398).
Next, run this on your command line:
pg_dump --host=<host_name> --port=<port> --username=<username> --password --dbname=<dbname> > output.sql
The terminal will ask for your password then run it and dump it into output.sql.
Then import it:
psql -d <heroku postgres string> -f output.sql
Note: in each place where you see <.....> in the above code, be sure to substitute your specific information. Thus you would put your username where it says <username>, and your heroku database url where it says <heroku postgres string>.
Similar answer here.

Related

How to stop the data base from being updated in production

I am new to Heroku, I have deployed a site from git but the issue is that every time I update the site code, the database also gets updated and all data from my localhost gets in the production, and the data of users from the site is lost. I don't use Heroku ClI and just have simply connected GitHub to Heroku. It's a Django app with a sqlite3 database. I tried .gitignore and git rm --cached too but didn't work. Please tell a way to stop the database file from getting updated when I push it.

Heroku: Importing data from Postgre dump Error

I am running Heroku Django app with post-gre. On my local machine, I have the same app with local db. Now wanted to import my data from local db to heroku db. I am following this guide. I have created a dump file from local db using:
PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mylocaldb > mylocaldb.dump
I have uploaded this dump file to Dropbox. On heroku terminal when I enter:
heroku pg:backups restore 'link/to/dump/file' DATABASE_URL
The output after selecting app is:
b008 ---restore---> HEROKU_POSTGRESQL_NAVY
Running... 32.8kB
It gets stuck on that XX.XkB and no progress after that. It drops all tables in my heroku db, and does nothing after that (I checked using psql).
My question is:
How do I check logs for this restore process ? (and hence track the error). heroku logs --tail shows nothing related to restore.
My local and heroku db have different name. Is that okay ?
My local db has lots of Users, foreign key, south migrations and admin logs. Is that okay or one has to remove all of them before dumping ?
Do I run "pg:backups restore" before or after running syncdb and and south migrate ?
You can run this command to get the info for the import:
heroku pg:backups info b008
I just ran into a similar issue where I uploaded the file to dropbox and used the share link as the backup URL. However, I failed to change the ?dl=0 param in the URL to 1 which means Heroku was trying to download an HTML file instead of the database dump.

Migrate Django development database (.sql3) to Heroku

How does one migrate their Django .sql3 development database to heroku?
Per here, and here I tried: heroku pg:psql --app sblic < database.sql3 but my Django admin shows no new uploads (even after syncdb/migrate/ or collectstatic
Perhaps there may be a way to directly upload an sql3 file to Heroku, but I went with the path of clearest certainty (convert local sql3 db to postgre db and upload a dump of postgre db to Heroku via pgbackups tool):
Create a dump of your sql3 database as a json file
With PostgreSql installed and with its bin directory in the Path environment variable, create a postgre user and database (or just plan on using your initial super user account created upon installing postgresql)
Update your settings.py with a reference to your newly created postgre database (note, 'HOST' may need to be set as 'localhost', 'user' is your postgre user login)
run python manage.py syncdb to initiate your new postgre db
Optional: if necessary, truncate your postgre db of contenttypes
load your dump from step 1 (if you have fixture issues, see here)
Now you have a working postgre local db. To migrate, first create a postgre dump
Post your postgre dump somewhere accessible by URL (such as drop box or amazon s3 as suggested in previous link).
Perform pgbackups restore command, referencing your dump url
Your heroku app should now reference the contents of your local db.
Heroku command line tool uses the psql binary. You have to install PostgreSQL on your local development machine to have psql available. From the (documentation)[https://devcenter.heroku.com/articles/heroku-postgresql#pg-psql]:
You must have PostgreSQL installed on your system to use heroku pg:psql.
You can in fact keep using your SQLite database with Heroku, but this is not recommended as it will be rewritten with your local copy if you re-deploy it to another dyno. Migrating the data to psql is recommended as described at https://devcenter.heroku.com/articles/sqlite3

Configuring postgresql database for local development in Django while using Heroku

I know there are a lot of questions floating around there relating to similar issues, but I think I have a specific flavor which hasn't been addressed yet. I'm attempting to create my local postgresql database so that I can do local development in addition to pushing to Heroku.
I have found basic answers on how to do this, for example (which I think is a wee bit outdated):
'#DATABASES = {'default': dj_database_url.config(default='postgres://fooname:barpass#localhost/dbname')}'
This solves the "ENGINE" is not configured error. However, when I run 'python manage.py syncdb' I get the following error:
'OperationalError: FATAL: password authentication failed for user "foo"
FATAL: password authentication failed for user "foo"'
This happens for all conceivable combinations of username/pass. So my ubuntu username/pass, my heroku username/pass, etc. Also this happens if I just try to take out the Heroku component and build it locally as if I was using postgresql while following the tutorial. Since I don't have a database yet, what the heck do those username/pass values refer to? Is the problem exactly that, that I need to create a database first? If so how?
As a side note I know I could get the db from heroku using the process outlined here: Should I have my Postgres directory right next to my project folder? If so, how?
But assuming I were to do so, where would the new db live, how would django know how to access it, and would I have the same user/pass problems?
Thanks a bunch.
Assuming you have postgres installed, connect via pgadmin or psql and create a new user. Then create a new database and with your new user as the owner. Make sure you can connect via psql with the new user into to the database. you will then need to set up an env variable in your postactivate file in your virtualenv's bin folder and save it. Here is what I have for the database:
export DATABASE_URL='postgres://{{username}}:{{password}}#localhost:5432/{{database}}'
Just a note: adding this value to your postactivate doesn't do anything. The file is not run upon saving. You will either need to run this at the $ prompt, or simply deactivate and active your virtualenv.
Your settings.py should read from this env var:
DATABASES = {'default': dj_database_url.config()}
You will then configure Heroku with their CLI tool to use your production database when deployed. Something like:
heroku config:set DATABASE_URL={{production value here}}
(if you don't have Heroku's CLI tool installed, you need to do it)
If you need to figure how exactly what that value you need for your production database, you can get it by logging into heroku's postgresql subdomain (at the time this is being written, it's https://postgres.heroku.com/) and selecting the db from the list and looking at the "Connection Settings : URL" value.
This way your same settings.py value will work for both local and production and you keep your usernames/passwords out of version control. They are just env config values.

Importing csv to heroku postgres

I succesfully deployed my first Django/Heroku app, and now I only need to transfer my database. It was previously on a MySql db on a Win7 PC. I looked around for ways to import csv into the Heroku db but didn't find anything. They suggest using a ruby gem to do it, or using taps and this command:
heroku db:push mysql://root:mypass#localhost/mydb.
My database is pretty small, only around 1000 columns and 2 tables, so it would be pretty simple to do it import the CSV files, but I cant find how to do it. Anyone knows?
Here's a few ideas that should get you going very quickly:
First, a quick and dirty approach:
Install Postgres on your local machine.
Import the CSV into a local Postgres database.
Push that database to Heroku.
Alternatively, a slightly less quick and still a little dirty approach:
Use the CSV reader Python module.
Create a task in your Django app that loads the CSV, iterates over each CSV row, creates a new corresponding model, and saves it in the server's database if the model is valid.
Run the task via the heroku CLI.
Once you're finished and you've verified the server database, you can remove the task and CSV from the repo.
Otherwise, taps may suit you well too!
As per enter link description here, you can use the copy command to load a CSV into postgres from you local filesystem. You should be able to use this with your Heroku DB with something similar to:
PGPASSWORD=passwordhere psql -h hostname -U username dbname -c "\copy ..."
You can import a local csv file as a table in your heroku postgres by the below command
PGPASSWORD=<your password> psql -h <your heroku host> -U <heroku user> <heroku postgres database name> -c "\copy bank (ifsc, bank_id, branch, address, city, district, state, bank_name) FROM '<local file path location>' CSV HEADER DELIMITER E'\t';"
Please alter the DELIMITER value according to your needs. The 'E' before delimiter value is to denote that the command contains escape characters, otherwise it wil; throw exception