django postgres could not connect to server - python

hi im working on a django project with virtualenv that is using the following
Django==1.7.6
argparse==1.2.1
psycopg2==2.6
wsgiref==0.1.2
but when i try python manage.py runserver it gives me this error
Performing system checks...
System check identified no issues (0 silenced).
Unhandled exception in thread started by <function wrapper at 0x7f0574e8c0c8>
Traceback (most recent call last):
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 222, in wrapper
fn(*args, **kwargs)
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
self.check_migrations()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 159, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 48, in __init__
self.build_graph()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 183, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.get_table_list(self.connection.cursor()):
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 165, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 138, in _cursor
self.ensure_connection()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 122, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 130, in get_new_connection
connection = Database.connect(**conn_params)
File "/home/marashen/.virtualenvs/192/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
my settings.py is
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'elfmsdb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
i also checked the postgresql.conf and it is using 5432 as port.
another odd thing is that i accidentally runserver outside my virtualenv and it worked but the django version is 1.6.
please help me, thanks a lot!

I dont know what the problem is but i tried
$sudo service postgresql restart
and runserver worked after that.

may be you can try with this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'elfmsdb',
'USER': 'postgres',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}

Related

Connecting Django to PostgreSQL database (GeoDjango)

I am having issues connecting to my Postgresql database with my Django website. My database is set up at Port 5434 as there is already an existing database in the default 5432 port.
This is the error I am getting
Traceback (most recent call last):
File "c:\users\anouphong\appdata\local\programs\python\python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "c:\users\anouphong\appdata\local\programs\python\python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\core\management\commands\runserver.py", line 127, in inner_run
self.check_migrations()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\core\management\base.py", line 505, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\loader.py", line 53, in __init__
self.build_graph()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\loader.py", line 223, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\recorder.py", line 77, in applied_migrations
if self.has_table():
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\migrations\recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 270, in cursor
return self._cursor()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 246, in _cursor
self.ensure_connection()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
self.connect()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 230, in ensure_connection
self.connect()
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\base\base.py", line 211, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\utils\asyncio.py", line 25, in inner
return func(*args, **kwargs)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\django\db\backends\postgresql\base.py", line 199, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Anouphong\.virtualenvs\project_homecook-l563S6IX\lib\site-packages\psycopg2\__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused (0x0000274D/10061)
Is the server running on that host and accepting TCP/IP connections?
This is my settings.py file in django
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'database_name',
'USER': 'postgres',
'PASSWORD': '******',
'HOST': 'localhost',
'PORT': '5434',
}
}
Hi, I figured it out. Really stupid mistake. I add two of the Database= {} configuration twice, once at the top of the file and once i the bottom and for got to delete it lol.
This issue is generated for lack of permission.You should give the permission in pg_hba file.
Log into your db user and then play the command psql. The sequensial coammnd is given bellow.
sudo su - postgres
psql
show hba_file;
In my case the output is /var/lib/postgresql/data/pg_hba.conf. Rember this location
\q
ctrl + d # for loging out from postgres user
sudo vim /var/lib/postgresql/data/pg_hba.conf #you can use your prefer text editor
You need to change the file as shown bellow. Just change all method to trust.

Why can't I run my Django project using PostgreSQL?

Firstly, I'm on Windows 10.
I just started learning Django. I did the tutorials that are with the Django documentation. I need to program an app that uses a PostgreSQL database. This is for a test following up a job interview, so I don't want to mess this up.
When I run the command "python manage.py runserver", I run into the following error :
d:\Utilisateur\Documents\code\code_python\Django\Psycle_test\mysite>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
The Django documentation says that I need to install psycopg2. Which I did using the command "pip install psycopg2". I configured settings.py in my Django project in order to use a PostgreSQL database :
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myDataBase',
'USER': 'myDataBaseUser',
'PASSWORD': 'password',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Changing 'ENGINE': 'django.db.backends.postgresql' to 'ENGINE': 'django.db.backends.postgresql_psycopg2' gives me the same error.
I also tried to change '127.0.0.1' to 'localhost' and it still doesn't work.
(The different topics I've seen talk about a postgresql.conf file, but I can't seem to find it anywhere, if it can even solve anything anyway.)
What am I doing wrong?
Edit
After installing PostgreSQL 9.6, creating myDataBase, creating a role myDataBaseUser and granting the privileges to that role, I now get that error:
d:\Utilisateur\Documents\code\code_python\Django\Psycle_test\mysite>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
self.check_migrations()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\core\management\base.py", line 453, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations
if self.has_table():
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor
return self._cursor()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor
self.ensure_connection()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
self.connect()
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python36\lib\site-packages\psycopg2\__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError
It seems like you dont have postgres database installed locally. Download it from here
Then going into console you need to create database by create database myDataBase;
create role by create role myDataBaseUser with password 'password'. Grant privileges to the role by
grant all privileges on database "myDataBase" to myDataBaseUser.
Then you will
be able to connect.

django connec mysql --_mysql_exceptions.OperationalError: (2006, <NULL>)

In fact, I learn the django from the website:https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial02/
The next is my settting.py
DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
# }
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_01',
'USER': 'root',
'PASSWORD': '9940',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
}
}
mysql :
enter image description here
I want to know how to solve this problem,or I don't know what's the problem?
Thank you very much!
I think DATABASES is right,
I want to connect mysql to django program named mysite, but I run 'python manage.py migrate':
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000000003B31D08>
Traceback (most recent call last):
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
self.connect()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\connections.py", line 208, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2006, <NULL>)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\core\management\commands\runserver.py", line 120, in inner_run
self.check_migrations()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\core\management\base.py", line 442, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\loader.py", line 212, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\recorder.py", line 61, in applied_migrations
if self.has_table():
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\migrations\recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 255, in cursor
return self._cursor()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 232, in _cursor
self.ensure_connection()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
self.connect()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 216, in ensure_connection
self.connect()
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\base\base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\django\db\backends\mysql\base.py", line 227, in get_new_connection
return Database.connect(**conn_params)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\__init__.py", line 85, in Connect
return Connection(*args, **kwargs)
File "D:\Tools\Anaconda3\envs\test-django\lib\site-packages\MySQLdb\connections.py", line 208, in __init__
super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (2006, <NULL>)
Maybe it's because django can't support the new password encryption of mysql. You should try changing it.
You can do this from the mysql command line client:
I got the same OperationalError: (2006, <NULL>) when using python to connect to MySQL Server on Windows. It seems like the error has something to do with the new authentication method in MySQL 8. I reconfigured the MySQL server from using the recommended authentication method to "Use Legacy Authentication Method" and the error goes away.

Not able to run 'python manage.py migrate'

I am new to programming and learning the web development. I have installed:
-Django 1.9
-postgresql 9.5
-psycopg2(using whl file) db api for the postgresql on Windows 7 os.
not a virtual environment.
I have set up the database connection in my settings.py file for my project as following.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Test',
'User': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
but when I am running migrate I am running in to following problem. I want to connect the postgres to my django
See the error below:
Traceback (most recent call last):
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\postgresql\base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)psycopg2.OperationalError
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\core\management\commands\migrate.py", line 89, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\loader.py", line 49, in __init__
self.build_graph()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\loader.py", line 176, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\migrations\recorder.py", line 52, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 231, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 204, in _cursor
self.ensure_connection()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\utils.py", line 95, in __exit__six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\utils\six.py", line 685, in reraise
raise value.with_traceback(tb)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 199, in ensure_connection
self.connect()
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\django\db\backends\base\base.py", line 171, in connect
self.connection = self.get_new_connection(conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\postgresql\base.py", line 175, in get_new_connection
connection = Database.connect(**conn_params)
File "C:\Users\agarwal.s.13\AppData\Local\Programs\Python\Python35-32\lib\site
-packages\psycopg2\__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError
I have checked with python script the psycopg2 is able to connect the database in postgresql but when I am running migrate from django project, its not working and getting above error.
Your database config:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Test',
'User': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
Should be:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'Test',
'USER': 'postgres',
'PASSWORD': 'admin',
'HOST': 'localhost',
'PORT': '5432',
}
}
Notice that User should be USER

Unable to apply django migrations to postgres database

My django project earlier used sqlite, but now we have changed it to use Postgresql. Here are the new settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'vader',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
I have created a db vader and user root as its owner. Now, when I try to apply migrations with:
python manage.py migrate
I get the following error:
(env1)sushil#Linux-Machine:~/Adomattic/Vader$ python manage.py migrate
[profile] heap size is 22.5 MB
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 63, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 17, in __init__
self.loader = MigrationLoader(self.connection)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 184, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 59, in applied_migrations
self.ensure_schema()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 49, in ensure_schema
if self.Migration._meta.db_table in self.connection.introspection.get_table_list(self.connection.cursor()):
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 165, in cursor
cursor = self.make_debug_cursor(self._cursor())
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 138, in _cursor
self.ensure_connection()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 133, in ensure_connection
self.connect()
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/backends/__init__.py", line 122, in connect
self.connection = self.get_new_connection(conn_params)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 134, in get_new_connection
return Database.connect(**conn_params)
File "/home/sushil/Adomattic/env1/local/lib/python2.7/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
django.db.utils.OperationalError: fe_sendauth: no password supplied
It is saying no password supplied, but I am not sure where to pass the password. Any ideas what I can do to fix this?
You need to add your database user password to your settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'vader',
'USER': 'root',
# Add password here
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '5432',
}
}
You need to remove PASSWORD, HOST and PORT like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'vader',
'USER': 'root'
}
}
Then do the migrate again.

Categories

Resources