5

P.s. THIS PROBLEM WAS SOLVED (I WAS USING WRONG PORT NUMBER)

I am trying to configure Postgresql with django 2.2 on WINDOWS OS but ending up getting error. Here is what I did to configure postgres for my project:

  1. Installed postgresql latest version with all default configuration and gave my password
  2. Created database in SQL Shell (psql) by doing
  • CREATE USER nouman;

  • CREATE DATABASE blog OWNER nouman;

  1. Then I updated settings.py file for the database as:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'blog',
        'USER': 'nouman',
        'PASSWORD': 'my password',
        'HOST': 'localhost',
        'PORT': '',
    }
}
  1. I installed psycopg2 by the command: "pip install psycopg2".
  2. But when I update the database by "python manage.py migrate" it gives this error:
Traceback (most recent call last):
  File "C:\Users\nouma\Desktop\djano2byexample\myenv\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection
    self.connect()
  File "C:\Users\nouma\Desktop\djano2byexample\myenv\lib\site-packages\django\db\backends\base\base.py", line 195, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\nouma\Desktop\djano2byexample\myenv\lib\site-packages\django\db\backends\postgresql\base.py", line 178, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\nouma\Desktop\djano2byexample\myenv\lib\site-packages\psycopg2\__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  password authentication failed for user "nouman"


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\nouma\Desktop\djano2byexample\myenv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()

-----------snip--------

  File "C:\Users\nouma\Desktop\djano2byexample\myenv\lib\site-packages\psycopg2\__init__.py", line 126, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  password authentication failed for user "nouman"

2 Answers 2

8

You need to create DB user with command

CREATE ROLE username WITH LOGIN PASSWORD 'quoted password';
CREATE DATABASE databasename;
GRANT ALL PRIVILEGES ON DATABASE databasename TO username;

read here

Sign up to request clarification or add additional context in comments.

4 Comments

I think this is for Mac OS but I am using Windows.
anyway. You created user with command CREATE USER nouman;. so it has not any password to login.Try to use command from my answer
Thanks for the help but being a beginner I was making a stupid simple mistake that is solved now.
@NoumanMalik Can you state what you did to solve the problem? What was the "stupid mistake"? Thanks!
1

A default PostgresSQL installation always includes the postgres superuser. Initially, you must connect to PostgreSQL as the postgres user until you create other users (which are also referred to as roles).

To create a PostgreSQL user, follow these steps:

  1. At the command line, type the following command as the server's root user: su - postgres

  2. You can now run commands as the PostgreSQL superuser. To create a user, type the following command: createuser --interactive --pwprompt

  3. Follow the instructions on command line. PostgreSQL creates the user with the settings you specified.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.