【问题标题】:Flask Sql-Alchemy, sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: PAM authentication failed for user 'abc'Flask Sqlalchemy,sqlalchemy.exc.OperationalError:(psycopg2.OperationalError)致命:用户“abc”的 PAM 身份验证失败
【发布时间】:2020-08-10 08:44:31
【问题描述】:

我正在编写一个烧瓶应用程序,它使用 flask-sqlalchemy 库与 AWS RDS 中的 postgres 服务器进行通信。

该应用程序只需用户名和密码即可正常工作。但是我创建了一个 IAM 用户并允许他使用令牌登录到 postgres(使用 boto3 的函数 'generate_db_auth_token()')。使用令牌登录得到以下错误。

'sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: 用户“abc”的 PAM 身份验证失败'

这是我与令牌一起使用的 SQLALCHEMY_DATABASE_URI,

"postgresql+psycopg2://"+get_conn()[0]['user']+":"+str(get_conn()[0]['password'])+"@"+get_conn()[0]['host']+":"+str(get_conn()[0]['port'])+"/common?sslmode=verify-ca&sslrootcert=/home/ec2-user/root.pem"

我在这里做错了吗。

【问题讨论】:

  • 这里的问题似乎不是SQLAlchemy。您可以提取 database_uri 并尝试使用 Postico、PGAdmin 或 DataGrip/PyCharm 等应用程序直接连接到它吗?
  • RDS 在私有子网中运行,我认为现在我无法使用这些应用程序进行连接。有趣的是使用 psycopg2.connect() 我甚至可以使用 IAM 令牌进行连接,但无法使用 sqlalchemy 获得相同的行为。您认为用于密码的令牌中的字符可能是问题吗?
  • 这很有可能,尤其是:@/等。尝试删除这些或将特殊字符限制为.,-_

标签: python postgresql sqlalchemy flask-sqlalchemy


【解决方案1】:

使用这个:

sqlalchemy.engine.url.URL.create(
    drivername='postgresql+psycopg2',
    username='postgres',
    password='password',
    host='localhost',
    port=5432,
    database='common'
)

【讨论】:

    【解决方案2】:

    问题在于 URL 构造,如果您使用 AWS 令牌进行身份验证,请使用它来构造 URL。

    sqlalchemy.engine.url.URL('postgresql+psycopg2', username='postgres', password='password', host='localhost', port=5432, database='common')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-17
      • 2017-09-25
      • 2020-04-10
      • 2012-07-05
      • 2020-05-04
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多