Connect to Postgres on Heroku with Django properly - django

I'm new to Django and Heroku.
I'm confused about how I should connect to Postgres database on Heroku from my Django app considering the fact that all the credentials and DATABASE_URL could be changed.
Firstly, to connect to my Postgres on Heroku I started by using environment variables and hardcoded them in my Heroku dashboard.
Then I figured out that it is a bad practice because the values can be changed.
I checked this guide by Heroku where they recommend adding this to settings:
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
With that, I added my DATABASE_URL to my .env file - because otherwise, the URL will be empty. Now I can get all the correct database credentials in my DATABASE that are the same as in my dashboard. So halfway there.
Then I deleted all the hardcoded environment variables from my Heroku dashboard.
Then when I tried to heroku run python src/manage.py migrate -a myapp data, I received an error:
django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
As I understand it, the problem is that it can't connect to the database (maybe because I deleted environment variables).
From what I saw on the internet - a lot of people in their guides on migrating to Postgres on Heroku use the hardcoded environment variables approach - which is a bad practice.
Otherwise, Heroku's guide doesn't show how specifically we should connect to the database with dynamically updated credentials.
Please advice.

Related

Heroku worker can't access database

I have a Django project hosted by Heroku with a (postgresql) database, an app that interacts with that database (on a web dyno), and a worker that should also interact with the database.
The app asks the worker to do long running tasks in the background, like some database queries. However, I'm having trouble implementing this. I think the problem is that the worker is not authorized to access the database. The specific error I'm seeing is:
app[worker.1]: ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
This doesn't make sense to me as an error coming from the worker. The settings are getting loaded fine- everything on the web dyno works as expected. The only thing I can figure is that the worker doesn't have access to the configured settings, including the database credentials.
How do I resolve this?
Edit:
Here's the Procfile:
web: gunicorn myapp.wsgi --log-file -
worker: python launchworker.py
As indicated there, the worker is launched as a Heroku worker, and it isn't run through the Django project at all (as I understand would be the case if it were a management command). This was the method recommended by Heroku.

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.

force heroku / django to use local database

I have a django based herokuapp site. I set everything up a long time ago and am now unable to get things working with a local instance of postgresql. In my settings file, I updated:
DATABASES['default'] = dj_database_url.config()
to work with a local database:
DATABASES['default'] = dj_database_url.config(default='postgres://localhost/appDB')
When running foreman, I can view the site, but the database is not currently populated (although I did create the empty DB). Running:
heroku run python manage.py dumpdata
Returns the contents of the remote (herokuapp) database, while a syncdb command results in "Installed 0 object(s) from 0 fixture(s)". So it looks like I'm still contacting the remote database. I'm pretty sure the postgresql DB is setup correctly locally; how can I force the app to use it?
I'm sure this is simple, but I haven't seen anything useful yet. I did try
export DATABASE_URL=postgres:///appDB
but that hasn't helped.
Cheers

Putting local Postregsql database in Heroku

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.

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.