0

Edit skip first part, I found out (well, they tell me) that the problem is in the database \edit

I Have a python+django project that I keep on two computers. I mean have two copy of the same project and sometimes I work on one, sometimes on the other. With copy-paste I lose the secret key in a settings.py (they were different but I overwrite). I don't know how generation and storage of secret keys works but to recover I start a new project, took the key and copied the files like this:

1) make a copy of the project

2) deleted the original project but kept the copy

3) started a new project with the same name (so, in the same folder like the original)

4) copied the new secret key

5) copied the files from the copy of the old project in the new project (so settings.py is lost and the new secret key with it, but I have a copy)

6) changed the secret key with the new secret key

But this don't works and it gives the same error like before:

System check identified no issues (0 silenced).
Unhandled exception in thread started by <function check_errors.<locals>.wrapper
at 0x0000000004ADAEA0>
Traceback (most recent call last):
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 216, in ensure_connection
    self.connect()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\po
stgresql\base.py", line 174, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\psycopg2\__init__.py"
, line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATALE:  autenticazione con password fallita per l'ut
ente "gm"


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

Traceback (most recent call last):
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\utils\autorelo
ad.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\core\managemen
t\commands\runserver.py", line 120, in inner_run
    self.check_migrations()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\core\managemen
t\base.py", line 442, in check_migrations
    executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
loader.py", line 49, in __init__
    self.build_graph()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
loader.py", line 209, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\migrations\
recorder.py", line 61, in applied_migrations
    if self.has_table():
  File "C:\Users\Fabio\Envs\possedimenti\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 "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 255, in cursor
    return self._cursor()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 232, in _cursor
    self.ensure_connection()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 216, in ensure_connection
    self.connect()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\utils.py",
line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 216, in ensure_connection
    self.connect()
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\ba
se\base.py", line 194, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\django\db\backends\po
stgresql\base.py", line 174, in get_new_connection
    connection = Database.connect(**conn_params)
  File "C:\Users\Fabio\Envs\possedimenti\lib\site-packages\psycopg2\__init__.py"
, line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATALE:  autenticazione con password fallita p
er l'utente "gm"

translated: authentication with password failed for user 'gm'

I'm not sure when it happened the first time but I have another problem, probabily linked: I can't access the database. Maybe this is the cause of the first problem and not the secret key.

I use PostgreSQL and I access it using the utility standard pgAdmin III. It gives me the initial screen with Servers: PostgreSQL 9.5, I right click on it and select connect, it ask me for password, I gives but it throws an error:

An error has occurred. Error connecting to the server: FATALE: autenticazione con password fallita per l'utente 'postgres'.

translated: authentication with password failed for user 'postgres'

I have created only one database and user so I have the standard postgres and my database 'possedimenti_db' with an user called 'gm'.

What can I do? I don't mind the data present, I can delete all but if it don't make me access I don't know how to delete.

Thank you

My settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'possedimenti_db',
        'USER': 'gm',
        'PASSWORD': database_key,
        'HOST': host_key,
        'PORT': '5432',
    }
}

database_key and host_key are imported from external file.

Also database_key is different from the password I use to access the database with pgAdmin. In my other pc it works. I tried to change it in the other password but it throws anyway the same error.

Edit You Can read the solution in the comments of the answer but the problem was that my user hadn't a password definited.

6
  • 1
    Django secret key has nothing to do with PostgreSQL password. Search for "postgres reset user/admin password". Commented Aug 27, 2018 at 10:41
  • Yes, but I don't know which is my problem: the secret key or the database password? I don't think to be using a wrong password. Anyway I will do the search you advice Commented Aug 27, 2018 at 10:45
  • Try to connect Postgres directly, not from Django. Check what is the settings file and/or .env file are actually in use when you are running Django app. Commented Aug 27, 2018 at 10:46
  • 1
    I don't know why you think any of this has anything to do with the secret key. The error clearly tells you what is wrong: the database password. It doesn't mention the secret key and that is not the problem. Commented Aug 27, 2018 at 10:49
  • 1
    I changed password in PostgreSQL (pg_hba.conf set to trust) and restart server (services.msc => restart). Reset pg_hba to md5. Now I have access to the database with pgAdmin but at runserver I have the same error yet. I add relevant setting above Commented Aug 27, 2018 at 13:26

1 Answer 1

1

Use 'django.db.backends.postgresql_psycopg2' instead of 'django.db.backends.postgresql'.

you have to install psycopg2. Use this command: pip install psycopg2

Here is the example:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', 
        'NAME': 'team_db',                     
        'USER': 'jason',                      
        'PASSWORD': 'abcd',                 
        'HOST': 'localhost',                    
        'PORT': '5432',                      
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

I already had psycopg2 installed. I made the change to engine but it makes no difference: same error.
The user gm must have all rights in postgres, he must be admin or super user. Is user gm a simple user or admin?
It was an user. No create database, no create roles, no superuser no replicato. I changed all these in yes and restarted the postgres server but nothing changed. It gives the same error. Have you seen the change in the op? The one about the password at the end.
Try to hard write the password and the host as follows: 'PASSWORD': 'abcd', 'HOST': 'localhost', If it works, it means your two variables: database_key, host_key are not callable. Do the test to see
Ok, Thank you I solved. My gm user hadn't a password definited. Before I was using another user and when I created gm I forgot to add the password. So the database have a password, gm have another for this in my settings.py I'm not using the same password I use to access the database.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.