django 2.1 + PostgreSQL 11 + Python 3.7 - Cannot do makemigrations - django

I'm trying to create a geospatial database with geodjango and postgis following the recommendations of the book : Python Geospatial development, 3rd Edition of Erik Westra, in order to do it I'm trying to configure my django database and to connect it to my PostgreSQL db.
After having launched my PostgreSQL database, I've created my django project and django apps. From then I'd like to apply makemigrations command to my shared app with :
python manage.py makemigrations shared
But then I've go the following error :
File "C:\Users\[...]\Anaconda3\lib\site-packages\psycopg2\__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
I've even tried to check migrations with showmigrations but it makes the same error message so I've absolutely no clue what's going on.
here's my settings.py file:
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'shapeeditor',
'USER': 'shapeeditor',
'PASSWORD': '(password)',
}
}
I've put (password) to hide the real one but I've checked it's the good one.
The NAME corresponds to the database name with a USER who has the same name
The shared app is written in INSTALLED_APPS so I've checked I didn't forget it.
I've looked at the many posts in StackOverflow about the error I got but it doesn't correspond to what I've facing here

After writing my own question I've found where it bugged...
my USER shapeeditor didn't have the privileges, so it couldn't work, just switched with postgres and I worked, I'll have to change privileges in order to make it work

Related

Test database for Django + Heroku. Error creating the test database: permission denied to create database

I'm trying to run the tests for my Django project. I wrote this project some time ago, I had different settings then and tests were passing. Now I changed settings and deployed it on Heroku with Heroku Postgres database. Everything works fine already except I can't run tests. I've tried many different settings and nothing worked. Most of the time I'm getting this error: permission denied to create database
My last setting is following the instruction from this article on medium
Basically I have added 2nd Heroku Postgres database, add settings like below (but with valid variables of my heroku databases):
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7osdssadag0ugv5',
'USER': 'lhwwasadqlgjra',
'PASSWORD': '1524f48a2ce41177c4ssdadasd3a11680b735302d14979d312ff36',
'HOST': 'ec2-54-75-2326-118.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7osdssadag0ugv5',
}
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'd7hasadas9hqts5',
'USER': 'nybkduadsdgqot',
'PASSWORD': 'bb535b9cdsfsdfdsfdsfac54851f267444dd8cc230b2a786ab9f446',
'HOST': 'ec2-54-247-132-38.eu-west-1.compute.amazonaws.com',
'PORT': 5432,
'TEST': {
'NAME': 'd7hasadas9hqts5',
}
}
}
Then run python manage.py test --keepdb in my venv. Then I get an error:
RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
RuntimeWarning
Got an error creating the test database: permission denied to create database
I have also tried what is advised in this article
Do you have any ideas what I could do about this error? I don't know Django well. I play with it from time to time.
I'm using: Python 3.6.9, Django 3.0.3, Heroku Postgresql Hobby Dev
EDIT:
I'm not sure if this is now an issue with my settings DATABASES.
Now when I commented out all my settings concerning DATABASES and I run python manage.py runserver my development server starts as normal and I have access to a database I set before (even after restarting a computer). This looks like actual settings don't have effect (??) Any thoughts?
Django version 3.0.3, using settings 'forumproject.settings'
Starting development server at http://127.0.0.1:8000/
Ok, I found out what it was. My database settings were not taken into account, even I had DEBUG=True because I had this line on the end of the settings:
# Activate Django-Heroku.
django_heroku.settings(locals())
After commenting this out the error permission denied to create database
goes away and I can run tests with
python manage.py test --keepdb
I'm surprised how always posting a question on stack overflow help me to find answer immediately after. I was running in circles

django.db.utils.OperationalError: FATAL: database does not exist (postgres / deploy to digitalocean)

I am trying to deploy a project with digital ocean. I followed the instructions found at https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04
Some of the important ones:
I ran:
postgres=# CREATE DATABASE jobzumoDB;
CREATE DATABASE
then:
postgres=# CREATE USER admin WITH PASSWORD '123';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE jobzumoDB TO admin;
GRANT
set the following in settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'jobzumoDB',
'USER':'admin',
'PASSWORD':'123',
'HOST':'localhost',
'PORT':'',
}
then tried to run:
~/jobzumo/manage.py makemigrations
and got:
File "/home/justin/jobzumo/env/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: database "jobzumoDB" does not exist
Two things:
ALLOWED_HOSTS = ['jobzumo.com', '142.93.184.125']
I have not yet connect jobzumo.com to digital ocean, but the IP address was copied from my droplet.
Also, I ran: pip install django gunicorn psycopg2 (from digitial ocean)
but a tutorial on youtube said it was very important to now
install psycopg2-binary instead, however, I did not do this as the video was veering far from digital ocean's tutorial.
Thanks for any help, after starting to understand django I didn't think deploying would be this much of a headscratcher.
Formalizing what we worked out in the comments as an answer, when you give postgres an unquoted string as an identifier, it forces it to lower-case. see this similar answer from pgsql-general mailing list. So the actual name of the database created by the command CREATE DATABASE jobzumoDB; is jobzumodb. to create a database named jobzumoDB it's necessary to use quotes, as in CREATE DATABASE "jobzumoDB";
Use:
NAME': 'jobzumodb',
Instead of:
NAME': 'jobzumoDB',

How to connect to snowflake database from Django framework

I'm new to Django and I'm trying to display the result that comes from a Snowflake database. I know that Django has multiple built-in database backend engines like: django.db.backends.postgresql and django.db.backends.mysql among the other few it supports.
Unfortunately, I couldn't find a proper way of configuring a database backend engine in the
settings.py
When I enter sqlalchemy or snowflake-sqlalchemy as the engine, I get this error:
Try using 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'postgresql', 'sqlite3'
My guess was to go with sqlalchemy as that's what I usually use to connect to Snowflake outside of Django but for some reason, it's not working properly.
I'd appreciate any guidance on that.
2022 update: There's now a Snowflake backend for Django funded by
Snowflake customers and implemented by Django's Tim Graham:
https://github.com/cedar-team/django-snowflake
From their docs:
Install and usage
Use the version of django-snowflake that corresponds to your version of Django. For example, to get the latest compatible release for Django 3.2.x:
pip install django-snowflake==3.2.*
The minor release number of Django doesn't correspond to the minor release number of django-snowflake. Use the latest minor release of each.
Configure the Django DATABASES setting similar to this:
DATABASES = {
'default': {
'ENGINE': 'django_snowflake',
'NAME': 'MY_DATABASE',
'SCHEMA': 'MY_SCHEME',
'WAREHOUSE': 'MY_WAREHOUSE',
'USER': 'my_user',
'PASSWORD': 'my_password',
'ACCOUNT': 'my_account',
},
}
Some of the discussion while implementing it:
https://groups.google.com/g/django-developers/c/po9dS-2h4lg/m/UeKBoL8dBgAJ?pli=1
You should install a custom Snowflake engine like the following ones. Note that, as of today, those are incomplete. Though, it should not be difficult to implement missing Django features by completing the operations.pyfile.
-> https://github.com/pricemoov/django-snowflake
or
-> https://pypi.org/project/django-snowflake-backend/
please install snowflake-connector-python .E.g. below
pip3 install snowflake-connector-python==1.8.1
Here is the code to connect from SQL Alchemy.
=====================================================================
#!/usr/bin/env python
from snowflake.sqlalchemy import URL
from sqlalchemy import create_engine
engine = create_engine(URL(
account = 'XXXX',
user = 'XXXX',
password = 'XXXXX',
database = 'XXXXXX',
schema = 'XXXXXX',
warehouse = 'XXXXX',
role='XXXXXXXX',
))
try:
connection = engine.connect()
connection.execute(
"CREATE OR REPLACE TABLE test_async(c1 TIMESTAMP_NTZ,c2 VARIANT)",_no_results=True)
finally:
connection.close()
engine.dispose()
=========================================================================

PostgreSQL & Heroku - cannot connect to the database

I'm trying to add a column to a table via my Django app with South but keep getting the following error upon running the python manage.py migrate <app name> command:
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: could not translate host name "ec2-107-21-99-105.comp
ute-1.amazonaws.com" to address: Temporary failure in name resolution
Does anybody have an idea why this is happening? I'm a newbie to both South AND the PostgreSQL database management system (which Heroku uses), so I am more than a bit confused.
Make sure you have defined your default database in settings.py like this:
DATABASES = {
'default': dj_database_url.config(default=os.environ.get('DATABASE_URL'))
}

South error when working with multiple databases: django.db.utils.ConnectionDoesNotExist: The connection foo doesn't exist

I have 2 Django projects with following db settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'db1', # 'db2' for second db
...
}
}
When trying to sync second db with command
python manage.py syncdb --database=db2
I receive error
django.db.utils.ConnectionDoesNotExist: The connection db2 doesn't exist
When I use some other commands, South uses migrations from first project and fills db2 with wrong tables. How to correctly sync/migrate several projects served by single Django + South instance?
The database syncing method does not take the NAME key in the --database option. As specified earlier, default for your db1 only works.
So you need to setup an additional database dictionary for your db2.