Django部署到Postgres期间发生身份验证错误

时间:2020-04-28 14:31:43

标签: python django postgresql

在迁移到我在Amazon服务器上新配置的数据库时遇到问题。 运行时

python manage.py makemigrations

我得到了错误:

   conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL:  password authentication failed for user "ubuntu"
FATAL:  password authentication failed for user "ubuntu"

坦率地说,我是一个初学者,我感到困惑,因为尽管我运行虚拟ubuntu机器来托管django应用程序,但我没有“ ubuntu”用户。

这是我的.env文件:

SECRET_KEY = secretkey..
DEBUG = False
NAME = dbexostock
USER = pierre
PASSWORD = mypassword
HOST = hostnameprovidedbyamazonrds

settings.py

    'default': {
        'ENGINE': 'django_tenants.postgresql_backend',
        'NAME': config('NAME'),
        'USER': config('USER'),
        'PASSWORD' : config('PASSWORD'),
        'HOST': config('HOST'),
        'PORT': '5432',
    }
}

和我的表单之一需要在数据库中编写,例如:

 engine = create_engine('postgresql://pierre:mypassword@:hostnameprovidedbyamazonrds:5432/dbexostock',
                               connect_args={'options': '-csearch_path={}'.format(dbschema)})
        metricsdb = metrics.to_sql('dashboard_metrics', engine, if_exists='replace')

我没有什么可以验证的,对于这个问题的任何帮助,我将不胜感激

1 个答案:

答案 0 :(得分:0)

如果是Postgres的全新安装,并假定您创建了数据库和用户,并授予了该用户对数据库的权限。尝试使用您创建的用户名和密码登录AWS实例,如果可以的话,这可能是您必须更改的Postgres设置。

在全新安装Postgres的情况下,默认情况下,身份验证设置为IDENT,这意味着您用来登录服务器的用户(可能是UBUNTU)是Postgres用户正在尝试使用。

要使Postgres使用您在settings.py文件中设置的用户名和密码,您必须编辑pg_hba.conf文件

首先登录Postgres,找到正在使用哪个pg_hba.conf文件的postgres $ psql postgres 然后运行: postgres=# SHOW hba_file 这将告诉您Postgres正在使用的设置文件的文件路径。

打开该文件进行编辑,并验证METHOD是否设置为md5而不是ident或其他名称。保存文件后,如果需要更改,请重新启动postgres。

# TYPE  DATABASE        USER            ADDRESS                 METHOD

 # "local" is for Unix domain socket connections only
 local   all             all                                     ident
 # IPv4 local connections:
 host    all             all             127.0.0.1/32            ident
 # IPv6 local connections:
 host    all             all             ::1/128                 ident
 # Allow replication connections from localhost, by a user with the
 # replication privilege.
 local   replication     all                                     ident
 host    replication     all             127.0.0.1/32            ident
 host    replication     all             ::1/128                 ident
相关问题