Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg2.OperationalError: FATAL: password authentication failed for user "nouman"

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"
like image 259
Nouman Javed Avatar asked Jan 25 '23 09:01

Nouman Javed


2 Answers

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

like image 116
Таалай Дюшебеков Avatar answered Jan 29 '23 15:01

Таалай Дюшебеков


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.

like image 41
engin_ipek Avatar answered Jan 29 '23 14:01

engin_ipek