我想使用gino和asyncpg将我的应用程序连接到heroku Postgress数据库

2024-05-15 15:47:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用gino和asyncpg将我的应用程序连接到heroku Postgress数据库。我已经指定了ssl模式,但它仍然给出错误

代码

import asyncpg
import asyncio
import certifi
from gino import Gino

db = Gino()


class aa(db.Model):
    __tablename__ = 'aa'

    id = db.Column(db.Integer(), primary_key=True)
    nickname = db.Column(db.Unicode(), default='noname')





DATABASE_URL = "postgres://wglubqaksikdhr:a821fe4468ce05cd7e82da39cdb1e80a552cf295c01abdac88f1b155e6be3cec@ec2-54-247-89-181.eu-west-1.compute.amazonaws.com:5432/d6o79ufd72jt6l"
async def main():
    await db.set_bind(DATABASE_URL, ssl = certifi.where(), sslmode ='require')
    await db.gino.create_all()
asyncio.get_event_loop().run_until_complete(main())```

Error
asyncpg.exceptions._base.InterfaceError: `sslmode` parameter must be one of: disable, allow, prefer, require, verify-ca, verify-full

Tags: importasynciourlssldbmaincolumnawait
1条回答
网友
1楼 · 发布于 2024-05-15 15:47:32

函数set_bind没有关键字sslmode。如果要将此关键字解析为asyncpg,可以在DB_DSN中设置它。这里是Asyncpg Document。例如:

DATABASE_URL = "postgres://user:pass@locahost:5432/schema?sslmode=require"

但这对我来说并不管用D添加此选项后,我无法连接到Heroku数据库

无论如何,这对我来说是有效的:

import ssl

ctx = ssl.create_default_context(cafile="")
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

await db.set_bind(DATABASE_URL, echo=True, ssl=ctx)
await db.gino.create_all()

我还建议您阅读psycopg2。因为Heroku支持这个,这里是document

相关问题 更多 >