Switch from SQLite to postgres, postgres asking for a password - django

Two things to note: One, I am executing these commands with my virtual environment running. So terminal appears as (env):. Two, every time I tried to enter su postgres, it asked me for a password.
I am reading a book on Django(using a mac), the text says:
"If you are using macOS, download PostgreSQL from https://www.postgresql.org/download/ and install it. You also need to install the Psycopg2 PostgreSQL adapter for Python. Run the following command to install it: pip install psycopg2==2.7.4."
I already had postgres installed on my machine, as I was previously fumbling with trying to deploy a website, before reverting back to this example project I had built. So I did not go to the website and install it. I installed psycopg2 as instructed ((env): pip install psycopg2==2.7.4.)
"Let's create a user for our PostgreSQL database. Open the shell and run the following commands: su postgres then createuser -dP blog"
I was thinking that "open the shell" meant enter the python virtual shell? So:
python manage.py shell
then
su postgres
this did not work, it asked me for a password. Exited the shell, ran the same command (env): su postgres, same result.
From there I thought maybe since I am in a virtual environment I do have to redownload postgres. I ran pip install postgres (did not know if this would download something, but it did, so I didn't bother going to the website/also would not know how to install it to this environment from the website). Note, this installed postgres as well as psycopg-someversion-binary. So now I have psycopg2==2.7.4 and some version of psycopg2-binary installed. From there I ran (env): su postgres (note from my environment). The same result, it asked for a password.
Then I reentered my python interactive shell with python manage.py shell and again ran su postgres. Same result.
At this point, I tried to research it on my own. What I read said I need to run su - postgres from root(?) by entering sudo -i at the terminal. I did this, now I am at Justins-MBP:~ root# (this does not seem it is at all related to the django project I am trying to use a postgres db for). Anyway, from here su - postgres and as you might have guessed, it asked me for a password.
Any help on how I can properly switch the db is greatly appreciated.
Also, after typing all of this and thinking about what I did I realized I haven't changed the database settings of the python project yet. But neither has the book, so not really sure how this project would even be aware of this database?

You probably have not yet set up the root user on MacOS, see this SO answer. However, for more information about the use of su, see this answer with a link to Apple's tech notes on how to create one.
The reason you should do this in your macOS terminal is because your setting up, configuring and doing database actions on a PostrgreSQL database, and this on its own, in the way your book is describing, is unrelated to Django (or Python for that matter).
So you have to make sure that 1) PostgreSQL is installed on your device and 2) that you have PostgreSQL running (as you will be creating users, which in fact means INSERTing data into your PostgreSQL Database). I used this blogpost (not affiliated) in the past to set up (install, run and configure) PostgreSQL on MacOS.
Regarding Django, you will need to change DATABASES in your settings.py (also see this part in Django tutorial)
DATABASES = {
# default namespace
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname' #name of the database,
'USER': 'username' #name of the created user,
'PASSWORD': 'password',
'HOST': '127.0.0.1' #when local,
'PORT': '5432' #default psql port,
}
}

Related

How to change the database local to be in a cloud (Django)

I wrote a project in django framework and I use the local DataBase SQLite, and I now want to upload it to the cloud so other people who work with me can access DataBase i.e. when I create a user then that user will exist with my co-workers.
Maybe someone has a guide on how to do this without modifying the DataBase so that I do not have to modify the rest of the code?
Ok i found the answer,
what you should do is Connect Django and MongoDB Using Djongo
you can read from here
I RECOMMEND TO FOLLOW MY SETPS.
go to monogodb site -> clickHere and create free account.
create new cluster
then you will reverse to this page if no in the left side press DataBase under DEPLOYMENT and then you get.
press on Connect buttom
choose "Connect your application" and then choose python and the last version
after all this you will have this link:
for example!!!
mongodb+srv://<username>:<password>#<atlas cluster>/<myFirstDatabase>?retryWrites=true&w=majority
copy this link and do the next setps:
go back to your terminal and install this package: pip install djongo
and open settings.py
change your DATEBASE to like this:
DATABASES = {
'default': {
'ENGINE': 'djongo',
'NAME': 'your-db-name',
'CLIENT': {
'host': 'mongodb+srv://<username>:<password>#<atlascluster>/<myFirstDatabase>?retryWrites=true&w=majority'
}
}
}
in 'host' paste the link you copy from monogodb site.
in 'NAME' write your name of the database.
Now that we have the Django project (and app), you can create the collections in MongoDB using the commands:
python manage.py makemigrations <app-name>
python manage.py migrate
if you get error like 'django.db.utils.databaseerror'
what you should to do is:
It deletes all the files that are in the migrations folder that are in the apps you have created and after that run again the command above.

AttributeError: 'DatabaseOperations' object has no attribute 'geo_db_type' QGIS Docker container to Heroku

There is a really old thread on stackoverflow here Getting 'DatabaseOperations' object has no attribute 'geo_db_type' error when doing a syncdb
but the difference that I have with their issue is that my containers have the POSTGIS and POSTGRES installed in. Specifically I used QGIS and the image is like so
db:
image: kartoza/postgis:13.0
volumes:
- postgis-data:/var/lib/postgresql
So locally I have two docker images - one is web and the other is the kartoza/postgis
I also have this as well in the settings.py file
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
which should support the GIS data. I see all my packages gis, geolocation packages installed with no issues. But I am getting the above error when I run heroku run python manage.py migrate
The website runs with very limited functionality as the geo variables are needed to get you past the landing page.
The steps I have taken to deploy is
heroku create appname
heroku stack:set container -a appname
heroku addons:create heroku-postgresql:hobby-dev -a appname
heroku git:remote -a appname
git push heroku main
EDIT The db url on heroku is postgres://foobar:3242q34rq2rq32rf3q2rfq2q2r3vq23rvq23vr#er3-13-234-91-69.compute-
I have also ran the below command and it shows that the db now takes GIS, but I still get the error
$ heroku pg:psql
create extension postgis;
try replacing db with localhost
heroku config:add DATABASE_URL=postgis://foo:bar#localhost:5432/gis
Ok so after 14 or so hours of reading. The issue here is that
Heroku database does NOT have qgis capabilities enabled as a default - you have to use heroku pg:psql then run CREATE EXTENSION postgis;
Having QGIS installed within the docker container results it not being able to perform functions such as hero pg:push localdatabase -postgresqlremoteherokudb and heroku config:add DATABASE_URL=postgis://foo:bar#localhost:5432/gis So if anyone is following the python nearbyshops tutorial, you might end up here as well. I got fixated here thinking it's a db issue, but it was more so to do with heroku configuration settings.
If you are coming here from docker, which might mean you need to install django_heroku package to modify your settings. There are recent 2019 and 2018 guides on how to deploy Geospatial apps to heroku, but again, you don't need those. (at least for me I didn't)
GIS now is available as an extension, you no longer need to add buildpacks.
There are still some configuration issues if you are using gunicorn for staticfiles, so the website at first won't launch properly. Highly suggest that you track your heroku log errors (manual settings.py coding or heroku addons).

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.

Restoring OSQA database to initial state

I installed OSQA using the bitnami installer and everything worked fine. Now, I am hacking at the osqa code base. If I need to restore the database to its initial state, do i need to reinstall OSQA or is there any command to truncate the database and load intial data.
I tried using use_osqa.bat and did a python.py manage migrate forum but it didnt work.
It uses a postgresql database
You can use the django-admin.py flush:
Returns the database to the state it was in immediately after syncdb was executed. This means that all data will be removed from the database, any post-synchronization handlers will be re-executed, and the initial_data fixture will be re-installed.
django-admin.py flush
Finally, this worked for me. Note that this is applicable only for OSQA using the bitnami environment with postgresql database.
if you want to restore your database state to the original version, first connect to postgres database using admin credentials(use any client, I used Dbeaver)
database : postgres
username : postgres
password : **admin password** : this is the same password you gave while installing bitnami-osqa
Now, drop the bitnami-osqa database and create it again(if you already have connections to bitnami_osqa, close them)
Drop-database bitnami_osqa;
Commit;
Create-database bitnami-osqa;
Commit;
Now open use_osqa.bat in your bitnami root folder and execute the following
cd apps\osqa
python manage.py syncdb --all --verbosity 2
[specify yes when it prompts to create super user and create some user]
python manage.py migrate forum --fake