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

时间:2020-07-02 10:59:25

标签: python heroku-postgres asyncpg gino

我想使用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

1 个答案:

答案 0 :(得分:0)

函数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