-2

I am new to programming and learning the web development. I have installed the -Django 1.9 -postgresql 9.5 -psycopg2(using whl file) db api for the postgresql on Windows 7 os.

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.

NaagRaaj
  • 31
  • 1
  • 3
  • You want help. You need to explain the problem. Not just giving an error without describing your setup – dkarchmer Mar 20 '16 at 17:37
  • @davka I am new to programming and learning the web development. i have installed the Django 1.9 and postgresql 9.5 and psycopg2 db api for the postgresql. Now I have set up the database connection in my setting.py file for my project as following. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', # 'NAME': 'Test', 'User': 'postgres', 'PASSWORD': 'admin', 'HOST': '', 'PORT': '', } but when I am running migrate I am running in to above problem. I want to connect the postgres to my django. – NaagRaaj Mar 20 '16 at 18:35

2 Answers2

0

Your postgres server is not started. You need to start the server first. Django does not come with postgres or any DB server. It provides connectors and ORM for these DB servers. Install postgres , create user and db as per your DATABASE settings in django, the errors should go. If you dont know how to install postgres, then google. Hope this helps.

Naveen Kumar
  • 189
  • 2
  • 5
  • Postgresql 9.5(with users and database) is installed on the machine and the server is running. I canconnect to it via a python script(create table or pass sql queries) but can not do migrate from django using 'migrate'. getting above error. – NaagRaaj Mar 20 '16 at 18:45
  • The settings does not have HOST. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'Test', 'User': 'postgres', 'PASSWORD': 'admin', 'HOST': 'localhost', 'PORT': '5432', } } – Naveen Kumar Mar 20 '16 at 18:52
  • its running on localserver. I have tried 'localhost' or '127.0.0.1' with default port '5432' for the db but none of its working. :( – NaagRaaj Mar 20 '16 at 18:53
  • Your post shows the wrong DB settings, there is no host. So I just asked. – Naveen Kumar Mar 20 '16 at 18:57
  • it is DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'Test', 'User': 'postgres', 'PASSWORD': 'admin', 'HOST': 'localhost', 'PORT': '5432', } } but same error – NaagRaaj Mar 20 '16 at 18:58
  • Are you using some kind of virtualenv to load django and its dependencies? – Naveen Kumar Mar 20 '16 at 18:58
  • No I am not running any virtualenv. I only have one version of Python and Django installed. – NaagRaaj Mar 20 '16 at 19:03
  • syncdb not supported in django 1.9 but instead migrate which is giving the error – NaagRaaj Mar 20 '16 at 19:24
0

You need to change 'User' to 'USER' in your DATABASES settings.

evergreen
  • 856
  • 6
  • 7