Issue with django DB connectivity on heroku - django

I am new to django and might be missing something obvious.
I intend to create a simple project and deploy on heroku.
Since sqlite3 is not supported on heroku, I have setup a postgres instance on elephantsql.
I have changed the settings.py file and able to connect to the postgres instance from http://127.0.0.1:8000/.
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "dummy",
"USER": "dummy",
"PASSWORD": "dummy",
"HOST": "dummy",
"PORT": "5432"
}
}
However, I am still not able to connect from heroku.
ProgrammingError at /
relation "home_signup" does not exist
LINE 1: ...e_signup"."email", "home_signup"."timestamp" FROM "home_sign...
^
I have gone through the postgres db which heroku offers, but the free version is limited to 10rows only.So, thought of using elephantsql which has better free offering.
Any pointer will be great help.
Some more details:
I created an instance in elephantsql > ran the migrations locally > checked using pgAdmin4 tool > table is created
The project just takes in text from an input field and adds to a col in the table.It is able to insert records without any issues locally.I am using pgadmin to check if the record is inserted or not.
I am not doing any select or any other db operation.

Use django-heroku.
I used this video to deploy my app: https://www.youtube.com/watch?v=6DI_7Zja8Zc

Finally I found the solution.
Heroku doesn't honor the database details as provided in settings.py
Heroku has a section called settings under its dashboard.
Under the config vars section, we need to add the DB TNS as a key value pair - with key=DATABASE_URL and value=DB TNS.
Screenshot:
Note: If you have a heroku provisioned postgre DB, the DATABASE_URL will point to the heroku postgre and we can't edit the value. We need to remove the postgre DB add-on first.

Related

Connect to Postgres on Heroku with Django properly

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.

django postgres integration error, no such table - auth user

After updating the django settings.py default database to correct values.
i am able to run makemigrations and migrate command and also create super user. but when i login to the admin it gives me error of “no such table - auth user, OPERATIONAL ERROR”.
I noticed that even if i delete db.sqlite3 file, it comes back when i try to login, i think django looks for table in db.sqlite3 and not postgres.
why db.sqlite3 file re appear after deleting ?
how do i correctly configure my settings.py ?
i am integration using digitalocean managed database services with django installed in one droplet, i have integrated both preciously without error but i installed postgres, this is the first time using managed database service.
Thanks
It seems that your django settings are still pointing to the SQlite database.
Did you reload your WSGI process ? If not, the old SQlite settings are still used in memory.

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

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

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.