I can import migrations with Sqlite3 in my Django project, but when I try to import it with postgreql, I get an error like this. How can I fix this?
I installed before
pip install psycopg2
pip install django psycopg2
Error
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
DB Settings Django
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'lordplusdb',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Try to use the following value for the engine:
'ENGINE': 'django.db.backends.postgresql'
The added _psycopg2 is for older versions of Django.
https://docs.djangoproject.com/en/3.0/internals/deprecation/#deprecation-removed-in-3-0
Related
I am trying to switch my Django database from SQLite3 to PostgreSQl, so I follow many tutorials to install and setup Postgres with Django project.
I did the following: pip install psycopg2, pip install psycopg2-binary and I modified the settings.py like that:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': BASE_DIR / 'db.postgresql',
'USER': 'muusername',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}
Finally I maked my database, by running the command python manage.py makemigrations.
However, I got this error:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
Please note that I am also seccesufully install the pgAdmin in my OS which is Windows 10 in a first step.
I know that the problem is related by configuration of Postgres in my django project, but I don't know how to fix it, also I checked my djnago version which is the latest one, also, all needed packages are installed in my venv.
You cannot add like this in postgres:
'NAME': BASE_DIR / 'db.postgresql', #You got that error because of this. This setting is for only sqlite3 not for postgres
Just add db_name in NAME:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'your_db_name', #add your db name here
'USER': 'postgres',
'PASSWORD': 'your_db_password',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Make sure you have actually downloaded the PostgreSQL installer itself and installed it.
I solve this problem by just upgrade the django, as you can see here in the exception:
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.postgresql' isn't an available database backend or couldn't be imported. Check the above exception. To use one of the built-in backends, use 'django.db.backends.XXX', where XXX is one of:
'mysql', 'oracle', 'sqlite3'
Django ask the user to use mysql, oracle or sqlite3, but not the case of postgresql.
So this exception will be fixed when the user upgrade the django version.
I hope this answer can be helpful for someone another.
This message showed after I executed runserver command:
System check identified no issues (0 silenced)
I have referenced this solution, but it didn't work for me(I start my mysql server by the command
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld"
).
Here is the background information(the "pip list" command):
(DjangoEnv) D:\pythonproject\DjangoEnv\loginoutonly>pip list
Package Version
Django 2.1.5
mysqlclient 1.3.13
pip 18.1
PyMySQL 0.9.3
pytz 2018.9
setuptools 40.6.3
wheel 0.32.3
Here is the db information in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'loginoutonly',
'USER': 'root',
'PASSWORD': 'pwd',
'HOST': 'localhost',
'PORT': '3306'
}
}
XAMPP helped me.
XAMPP says that
Port 3306 in use by "Unable to open process"!
I change my MySQL port to 3307 using XAMPP [Config] button.
Then, I change my settings.py code to
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'loginoutonly',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3307'
}
}
I also need to test the MySQL WorkBench connection of this newly connected port "3307".
I'm trying to make the migrations between my database (MySQL) and Django. I used the same parameters in Linux and didn't have any problems. Now I'm using the command :
python manage.py migrate
and I get nothing at all on the terminal. However, the command works if I let the default parameters ( for a sqlite database ). Also, I've noticed that it actually reads the 'settings.py' file because it returns an error if I write something that doesn't make any sense. Here are my parameters, I know for sure that they are correct ( I checked with the MySQL commands ).
settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my_db',
'USER': 'root',
'PASSWORD': '123456789_dont_judge_me',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
Error message
I'm using Windows 10, Python 3.5 and Django 2.0.
Do you have any idea where the problem could come from ?
Thank you for your responses kind people !
You can try this.
Note: I am not claiming it is best solution, but it is working for me
in case of Window10.
Step1 : Install pymysql
pip install pymysql
Step2: Edit settings.py
settings.py
---------------
import os
import pymysql
pymysql.install_as_MySQLdb()
import MySQLdb
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_test',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
Step3: migrate it
python manage.py migrate
you can follow this link
I have made a virtual environment and installed following:
sudo apt-get install libmysqlclient-dev
sudo pip install MySQL-python
and my setting is :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': 'localhost',
'PORT': '3306',
}
}
and when I do manage.py syncdb it gives the error saying 2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
I dont know whats wrong in here...
I searched for it... Mostly I found if I use virtualenvironment then the above setting is enough... Whats wrong in here ???
Install mysql server
sudo apt-get install mysql-server
then create a database for your purposes:
mysql -u root -p --execute "create database DB_NAME; grant all on DB_NAME.* to DB_USER#localhost identified by 'DB_PASSWORD';"
then you should be fine.
I should point out that the confusion of mysql searching for a socket, even if specifying a port, is because mysql defaults to a socket when specifying localhost as an address, if you want to use tcp/ip then you should put an address like 127.0.0.1 there.
Your HOST should change to '127.0.0.1', you must start service, especially if you are using xamp or lamp.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'DB_NAME',
'USER': 'DB_USER',
'PASSWORD': 'DB_PASSWORD',
'HOST': '127.0.0.1',
'PORT': '3306',
} #Your HOST should change to '127.0.0.1', you must start service, especially if you are using xamp or lamp
I have successfully followed the Django 1.4 Tutorial 1-4 (the poll system) and now its running until I deploy it in Apache Webserver 2.2 using Postgres Database. Everytime I access localhost I always have "Internal Server Error" but if I use MySQL as my database everything is ok. What do you think is wrong? Did I miss something to configure in Postgre?
Here is my database connection settings in Mysql(Working) and in Postgre(Not Working), both using Apache2.2 + mod_wsgi.so
//Postgre
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'django_demo1',
'USER': 'postgres',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '',
}
}
//MySQL
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_demo1',
'USER': 'root',
'PASSWORD': 'mypassword',
'HOST': 'localhost',
'PORT': '',
}
}
Other INFO:
OS: windows 7, Python: 2.7, Django: 1.4, Postgre 9.1
If you are able to access MySQL but not Postgres, I guess psycopg2 which is the Postgres adapter for python is not installed. Install psycopg2 using pip.
pip install psycopg2
Here is the pypi.