Python postgresql:致命:用户“douglas”的密码身份验证失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43060857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:27:47  来源:igfitidea点击:

postgresql: FATAL: password authentication failed for user "douglas"

pythondjangopostgresql

提问by Douglas da Silva

I am trying to migrate a DB from sqlite to postgresql...so I typed:

我正在尝试将数据库从 sqlite 迁移到 postgresql ......所以我输入:

sudo -u postgres psql
postgres=# ALTER USER postgres WITH PASSWORD 'newpassword';

and the output returns ALTER ROLE

并且输出返回 ALTER ROLE

but when I type python manage.py migrateI receive always the same error:

但是当我输入时,python manage.py migrate我总是收到相同的错误:

django.db.utils.OperationalError: FATAL: password authentication failed for user "douglas"

django.db.utils.OperationalError:致命:用户“douglas”的密码认证失败

This is the database sections of my settings.py.

这是我的 settings.py 的数据库部分。

# Old, using mysqlite
"""
DATABASES = {
    #'default': {
    #    'ENGINE': 'django.db.backends.sqlite3',
    #    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    #}
    'default': dj_database_url.config(default='postgres://localhost:5432/postgres_db_name'),
}
"""

# New, using postgres
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'douglas_db',
        'USER': 'douglas',
        'PASSWORD': 'vamointer',
        'HOST': '127.0.0.1',
        'PORT': '5432',
    }
}

Note: When I run the 'ALTER USER postgres WITH PASSWORD' I put in the same password defined in the settings.py.

注意:当我运行“ ALTER USER postgres WITH PASSWORD”时,我输入了在 settings.py 中定义的相同密码。

采纳答案by Shadow

The SQL you are running does not match the user you are attempting to use.

您正在运行的 SQL 与您尝试使用的用户不匹配。

You will need to create the userif it does not exist:

如果用户不存在,您将需要创建该用户

CREATE USER douglas WITH PASSWORD 'vamointer';

or if it does exist, change that user's passwordinstead.

或者如果确实存在,请改为更改该用户的密码

ALTER USER douglas WITH PASSWORD 'vamointer';

Once you have done that you should have more luck. You may need to assign permissions to that useras well.

一旦你这样做了,你应该有更多的运气。您可能还需要为该用户分配权限

回答by HAL 9000

Special characters in postgresql are converted to different characters while execution. Make sure you do not have special characters (#,$,etc..) in your password.

postgresql 中的特殊字符在执行时会转换为不同的字符。确保您的密码中没有特殊字符(#、$ 等)。

If you do, change the postgresql password as follows:

如果这样做,请按如下方式更改 postgresql 密码:

sudo -u postgresql psql
postgresql=#ALTER USER yourusername WITH PASSWORD 
'set_new_password_without_special_character';

Make sure you do not forget the ;at the end of postgresql command. Then run python manage.pyand it should work!

确保不要忘记;postgresql 命令末尾的 。然后运行python manage.py,它应该可以工作!

回答by Harlin

If you are bone-headed like me and have used 'USERNAME' instead of 'USER' in your Django database configs in settings.py, make sure you change it to 'USER' else you will see this same error. Hope this helps someone like me down the road.

如果您像我一样头脑发热,并且在 settings.py 的 Django 数据库配置中使用了“USERNAME”而不是“USER”,请确保将其更改为“USER”,否则您将看到同样的错误。希望这可以帮助像我这样的人。

回答by user12228212

You can try this:

你可以试试这个:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'telusko',
    'USER': 'postgres', #in place we should always be write USER only if we write USENAME then it will give error.
    'PASSWORD': '1234',
    'HOST':'localhost',
}}

回答by Sarmad Gulzar

In my case, I had to change

就我而言,我不得不改变

'ENGINE': 'django.db.backends.postgresql_psycopg2',

to

'ENGINE': 'django.db.backends.postgresql',

Hope it helps someone!

希望它可以帮助某人!